MysqlRootTrait uses trait:short, trait:short, trait:short
Provides support for accessing and initializing a root-level MySQL administrative model, typically used to perform high-privilege operations via a separate `MysqlModel` instance.
This trait depends on a PSR-11 compatible container and expects a MysqlModel
instance
to be provided directly or by reference (as a container service name).
Tags
Table of Contents
Constants
- DEFER_ASSIGNMENT = 'deferAssignment'
- The 'deferAssignment' parameter constant.
- MYSQL_ROOT = 'mysqlRoot'
- The 'mysqlRoot' parameter value.
- PDO = 'pdo'
- The 'pdo' parameter constant.
- SCHEMA = 'schema'
- The 'schema' parameter constant.
Properties
- $container : Container
- The DI container reference.
- $deferAssignment : bool|null
- Indicates if the the constructor is called before setting properties.
- $mysqlRoot : MysqlModel|null
- The mysql root model reference.
- $pdo : PDO|null
- The PDO reference.
- $schema : string|mixed|null
- The internal schema to use in the PDO fetch processes.
Methods
- bindValues() : void
- Bind named parameters to a prepared PDO statement.
- fetch() : mixed|null
- Execute a SELECT query and fetch a single result.
- fetchAll() : array<string|int, mixed>
- Execute a SELECT query and fetch all results.
- fetchColumn() : mixed
- Execute a query and return the value of a single column from the first row.
- fetchColumnArray() : array<int, string>
- Fetch a list of single-column results.
- initializeDefaultFetchMode() : void
- Set the default fetch mode on the statement.
- initializeMysqlRoot() : void
- Initialize the Mysql model reference.
- initPDO() : PDO|null
- Initialize the PDO instance from a config array or dependency injection container.
- isConnected() : bool
- Indicates if the PDO is connected.
- assertHost() : void
- Validates a MySQL host string.
- assertIdentifier() : void
- Validates a MySQL identifier such as a database name, user name, or table name.
Constants
DEFER_ASSIGNMENT
The 'deferAssignment' parameter constant.
public
mixed
DEFER_ASSIGNMENT
= 'deferAssignment'
MYSQL_ROOT
The 'mysqlRoot' parameter value.
public
mixed
MYSQL_ROOT
= 'mysqlRoot'
PDO
The 'pdo' parameter constant.
public
mixed
PDO
= 'pdo'
SCHEMA
The 'schema' parameter constant.
public
mixed
SCHEMA
= 'schema'
Properties
$container
The DI container reference.
public
Container
$container
$deferAssignment
Indicates if the the constructor is called before setting properties.
public
bool|null
$deferAssignment
= false
Only if the schema property is defined.
$mysqlRoot
The mysql root model reference.
public
MysqlModel|null
$mysqlRoot
$pdo
The PDO reference.
public
PDO|null
$pdo
= null
$schema
The internal schema to use in the PDO fetch processes.
public
string|mixed|null
$schema
= null
Methods
bindValues()
Bind named parameters to a prepared PDO statement.
public
bindValues(PDOStatement $statement[, array<string|int, mixed> $bindVars = [] ]) : void
Parameters
- $statement : PDOStatement
-
The PDO statement.
- $bindVars : array<string|int, mixed> = []
-
Associative array of bindings. Supports:
- ['id' => 5]
- ['id' => [5, PDO::PARAM_INT]]
fetch()
Execute a SELECT query and fetch a single result.
public
fetch(string $query[, array<string|int, mixed> $bindVars = [] ]) : mixed|null
The result is returned as an object or as a mapped schema class if defined. Alteration is applied via AlterDocumentTrait.
Parameters
- $query : string
-
The SQL query to execute.
- $bindVars : array<string|int, mixed> = []
-
Optional bindings for the query.
Tags
Return values
mixed|null —The result object or null if not found.
fetchAll()
Execute a SELECT query and fetch all results.
public
fetchAll(string $query[, array<string|int, mixed> $bindVars = [] ]) : array<string|int, mixed>
Results are returned as an array of associative arrays or schema instances. Alteration is applied via AlterDocumentTrait.
Parameters
- $query : string
-
The SQL query to execute.
- $bindVars : array<string|int, mixed> = []
-
Optional bindings for the query.
Tags
Return values
array<string|int, mixed> —An array of results.
fetchColumn()
Execute a query and return the value of a single column from the first row.
public
fetchColumn(string $query[, array<string|int, mixed> $bindVars = [] ][, int $column = 0 ]) : mixed
Parameters
- $query : string
-
The SQL query to execute.
- $bindVars : array<string|int, mixed> = []
-
Optional bindings for the query.
- $column : int = 0
-
Column index (0-based) to return from the first row.
Return values
mixed —The column value or 0 if the query fails.
fetchColumnArray()
Fetch a list of single-column results.
public
fetchColumnArray(string $query[, array<string|int, mixed> $bindVars = [] ]) : array<int, string>
Parameters
- $query : string
-
The SQL query to execute.
- $bindVars : array<string|int, mixed> = []
-
Optional bindings for the query.
Return values
array<int, string>initializeDefaultFetchMode()
Set the default fetch mode on the statement.
public
initializeDefaultFetchMode(PDOStatement $statement) : void
Uses FETCH_ASSOC by default or FETCH_CLASS (with optional FETCH_PROPS_LATE) if a schema class is defined and exists.
Parameters
- $statement : PDOStatement
-
The PDO statement to configure.
initializeMysqlRoot()
Initialize the Mysql model reference.
public
initializeMysqlRoot([array<string|int, mixed> $init = [] ]) : void
Parameters
- $init : array<string|int, mixed> = []
Tags
initPDO()
Initialize the PDO instance from a config array or dependency injection container.
public
initPDO([array<string|int, mixed> $init = [] ][, Container|null $container = null ]) : PDO|null
Parameters
- $init : array<string|int, mixed> = []
-
Configuration array. Expects Param::PDO as key.
- $container : Container|null = null
-
Optional DI container to resolve the PDO service.
Tags
Return values
PDO|null —The resolved PDO instance or null.
isConnected()
Indicates if the PDO is connected.
public
isConnected() : bool
Return values
boolassertHost()
Validates a MySQL host string.
protected
assertHost(string $host) : void
A valid host string may contain:
- letters (a–z, A–Z)
- digits (0–9)
- dots (.)
- hyphens (-)
- underscores (_) and percent signs (%) for wildcards
Parameters
- $host : string
-
The host name or IP to validate (e.g., 'localhost', '127.0.0.1', '%.example.com').
Tags
assertIdentifier()
Validates a MySQL identifier such as a database name, user name, or table name.
protected
assertIdentifier(string $name) : void
A valid identifier consists of letters (a–z, A–Z), digits (0–9), and underscores (_). This ensures safe usage in SQL queries without risk of injection or syntax errors.
Parameters
- $name : string
-
The identifier to validate.