Oihana PHP System

PDOModel 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
throws
ContainerExceptionInterface

If there is a problem retrieving services from the container.

throws
NotFoundExceptionInterface

If a required service is not found in the container.

example
use DI\Container;
use oihana\models\PDOModel;

$container = new Container();

// Configuration array with optional parameters
$config =
[
    'deferAssignment' => true,
    'pdo'             => 'my_pdo_service', // or a PDO instance
    'schema'          => MyEntity::class,
];

// Instantiate the model with the container and configuration
$model = new PDOModel( $container , $config ) ;

// Fetch a single record
$record = $model->fetch('SELECT * FROM users WHERE id = :id', ['id' => 123]);

// Fetch all records
$records = $model->fetchAll('SELECT * FROM users');
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Properties

$container  : Container
The DI container reference.
$deferAssignment  : bool|null
Indicates if the the constructor is called before setting properties.
$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.
bindValues()  : void
Bind named parameters to a prepared PDO statement.
fetch()  : mixed
fetchAll()  : array<string|int, mixed>
fetchColumn()  : mixed
initializeDefaultFetchMode()  : void
Set the default fetch mode on the statement.
initPDO()  : PDO|null
Initialize the PDO instance from a config array or dependency injection container.

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.

$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[, array<string|int, mixed> $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 : array<string|int, mixed> = []

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
throws
ContainerExceptionInterface

If container service retrieval fails.

throws
NotFoundExceptionInterface

If container service not found.

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'

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.

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
throws
ContainerExceptionInterface
throws
NotFoundExceptionInterface
Return values
PDO|null

The resolved PDO instance or null.


        
On this page

Search results