PDOModel extends Model uses PDOTrait
A base model class that integrates a PDO instance with dependency injection container support.
This class uses the PDOTrait to provide PDO-related database operations, binding, and fetching.
The model can be initialized with configuration options such as alters, binds, schema, defer assignment, logger, mock objects, and the PDO instance itself.
Tags
Table of Contents
Constants
- DEBUG = 'debug'
- The 'debug' parameter constant.
- DEFER_ASSIGNMENT = 'deferAssignment'
- The 'deferAssignment' parameter constant.
- MOCK = 'mock'
- The 'mock' parameter constant.
- PDO = 'pdo'
- The 'pdo' parameter constant.
- SCHEMA = 'schema'
- The 'schema' parameter constant.
Properties
- $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
- __construct() : mixed
- Creates a new PDOModel instance.
- __toString() : string
- Returns a String representation of the object.
- bindValues() : void
- Bind named parameters to a prepared PDO statement.
- fetch() : mixed
- fetchAll() : array<string|int, mixed>
- fetchColumn() : mixed
- fetchColumnArray() : array<int, string>
- Fetch a list of single-column results.
- 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.
- isConnected() : bool
- Indicates if the PDO is connected.
- isMock() : bool
- Indicates if the document use the mock mode.
Constants
DEBUG
The 'debug' parameter constant.
public
mixed
DEBUG
= 'debug'
DEFER_ASSIGNMENT
The 'deferAssignment' parameter constant.
public
mixed
DEFER_ASSIGNMENT
= 'deferAssignment'
MOCK
The 'mock' parameter constant.
public
mixed
MOCK
= 'mock'
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
The dependency injection container instance.
$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
__construct()
Creates a new PDOModel instance.
public
__construct(Container $container[, PDO|string|null} $init = [] ]) : mixed
Sets internal properties from the provided configuration array and initializes logger, mock, and PDO.
Parameters
- $container : Container
-
The DI container to retrieve services like PDO and logger.
- $init : PDO|string|null} = []
-
Optional initialization array with keys:
- Param::ALTERS: array of alterations to apply
- Param::BINDS: array of binds for queries
- Param::DEFER_ASSIGNMENT: bool whether to defer property assignment on fetch
- Param::SCHEMA: string class name of schema for fetch mode
- Param::PDO: PDO instance or service name in container
Tags
__toString()
Returns a String representation of the object.
public
__toString() : string
Tags
Return values
string —A string representation of the object.
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()
public
fetch(string $query[, array<string|int, mixed> $bindVars = = '[]' ]) : mixed
Fetch a single record from the database.
Parameters
- $query : string
- $bindVars : array<string|int, mixed> = = '[]'
fetchAll()
public
fetchAll(string $query[, array<string|int, mixed> $bindVars = = '[]' ]) : array<string|int, mixed>
Fetch all matching records from the database.
Parameters
- $query : string
- $bindVars : array<string|int, mixed> = = '[]'
Return values
array<string|int, mixed>fetchColumn()
public
fetchColumn(string $query[, array<string|int, mixed> $bindVars = = '[]' ][, int $column = = '0' ]) : mixed
Fetch a single column from the first row.
Parameters
- $query : string
- $bindVars : array<string|int, mixed> = = '[]'
- $column : int = = '0'
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.
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.
isConnected()
Indicates if the PDO is connected.
public
isConnected() : bool
Return values
boolisMock()
Indicates if the document use the mock mode.
public
isMock([array<string|int, mixed> $init = [] ]) : bool
Parameters
- $init : array<string|int, mixed> = []