PDOTrait uses trait:short, trait:short, trait:short, trait:short, trait:short
Provides methods for binding values, executing queries, and retrieving results using PDO.
Supports schema-based result mapping and integration with dependency injection.
Requires the following traits:
- AlterDocumentTrait
- BindsTrait
- DebugTrait
- ToStringTrait
Tags
Table of Contents
Properties
- $alters : array<string|int, mixed>
- The enumeration of all definitions to alter on the array or object key/value properties.
- $binds : array<string|int, mixed>|null
- The default bind values definition of the model.
- $container : Container
- The DI container reference.
- $debug : bool
- Indicates if use the debug mode.
- $deferAssignment : bool|null
- Indicates if the the constructor is called before setting properties.
- $mock : bool
- The mock flag to test the model.
- $pdo : PDO|null
- The PDO reference.
- $schema : string|mixed|null
- The internal schema to use in the PDO fetch processes.
Methods
- __toString() : string
- Returns a String representation of the object.
- alter() : mixed
- Alters the given document (array or object) based on the configured `$alters` definitions.
- alterAssociativeArray() : array<string|int, mixed>
- Alter the passed-in array.
- alterObject() : mixed
- Alter the passed-in object.
- alterProperty() : array<string|int, mixed>|object
- Alters a specific property of the given document (array or object) using a defined transformation rule.
- 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.
- initializeDefaultFetchMode() : void
- Set the default fetch mode on the statement.
- initializeMock() : bool
- Initialize the mock flag.
- initPDO() : PDO|null
- Initialize the PDO instance from a config array or dependency injection container.
- isMock() : bool
- Indicates if the document use the mock mode.
- prepareBindVars() : array<string|int, mixed>
- Prepares the binding parameters to inject in a PDO statement.
Properties
$alters
The enumeration of all definitions to alter on the array or object key/value properties.
public
array<string|int, mixed>
$alters
= []
$binds
The default bind values definition of the model.
public
array<string|int, mixed>|null
$binds
= []
$container
The DI container reference.
public
Container
$container
$debug
Indicates if use the debug mode.
public
bool
$debug
= false
$deferAssignment
Indicates if the the constructor is called before setting properties.
public
bool|null
$deferAssignment
= false
Only if the schema property is defined.
$mock
The mock flag to test the model.
public
bool
$mock
= false
$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
__toString()
Returns a String representation of the object.
public
__toString() : string
Tags
Return values
string —A string representation of the object.
alter()
Alters the given document (array or object) based on the configured `$alters` definitions.
public
alter(mixed $document) : mixed
This method determines the structure of the document and applies the appropriate transformation logic:
- If the document is an associative array, each key listed in
$alters
is processed usingalterAssociativeArray()
. - If the document is a sequential array (i.e., a list of items), the alteration is recursively applied to each item.
- If the document is an object, its public properties are altered using
alterObject()
. - If
$alters
is empty or no matching keys/properties are found, the document is returned unchanged.
Parameters
- $document : mixed
-
The input to alter. Can be an associative array, object, or a list of items.
Tags
Return values
mixed —The altered document, same structure as input.
alterAssociativeArray()
Alter the passed-in array.
public
alterAssociativeArray(array<string|int, mixed> $document) : array<string|int, mixed>
Parameters
- $document : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>alterObject()
Alter the passed-in object.
public
alterObject(object $document) : mixed
Parameters
- $document : object
Tags
alterProperty()
Alters a specific property of the given document (array or object) using a defined transformation rule.
public
alterProperty(string $key, array<string|int, mixed>|object $document, string|array<string|int, mixed> $definition[, bool|null $isArray = null ]) : array<string|int, mixed>|object
The transformation is defined by the $definition
argument, which can be either:
- A string representing a single
Alter::
constant (e.g.Alter::FLOAT
) - An array, where the first element is an
Alter::
constant and the rest are parameters for that transformation
If the alteration modifies the value, the altered value is set back into the document. Otherwise, the original document is returned unmodified.
Supported alter types:
- Alter::ARRAY — Explodes a string into an array (using
;
) and applies sub-alters - Alter::CALL — Calls a user-defined callable on the value
- Alter::CLEAN — Removes empty (
""
) or null elements from arrays - Alter::FLOAT — Casts the value to float
- Alter::GET — Resolves a document by ID using a model
- Alter::INT — Casts the value to integer
- Alter::JSON_PARSE — Parses a JSON string into a PHP value
- Alter::JSON_STRINGIFY — Encodes a value into a JSON string
- Alter::URL — Generates a URL based on document properties
- Alter::VALUE — Replaces the value with a fixed constant
Parameters
- $key : string
-
The name of the property to alter (e.g. 'price', 'tags')
- $document : array<string|int, mixed>|object
-
The document (array or object) passed by reference
- $definition : string|array<string|int, mixed>
-
The alteration definition: either a string (
Alter::
) or an array ([ Alter::X , ...args ]
) - $isArray : bool|null = null
-
Optional flag to indicate the type of document. If null, it will be inferred automatically.
Tags
Return values
array<string|int, mixed>|object —The altered document (same reference type as input)
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.
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.
initializeMock()
Initialize the mock flag.
public
initializeMock([array<string|int, mixed> $init = [] ]) : bool
Parameters
- $init : array<string|int, mixed> = []
Return values
boolinitPDO()
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.
isMock()
Indicates if the document use the mock mode.
public
isMock([array<string|int, mixed> $init = [] ]) : bool
Parameters
- $init : array<string|int, mixed> = []
Return values
boolprepareBindVars()
Prepares the binding parameters to inject in a PDO statement.
public
prepareBindVars([array<string|int, mixed> $init = [] ]) : array<string|int, mixed>
Parameters
- $init : array<string|int, mixed> = []
-
The binding parameters to push in the default binds associative array definition.