Oihana PHP System

Model uses DebugTrait, ToStringTrait

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

Constants

DEBUG  = 'debug'
The 'debug' parameter constant.
MOCK  = 'mock'
The 'mock' parameter constant.

Properties

$container  : Container
The DI container reference.
$debug  : bool
Indicates if use the debug mode.
$mock  : bool
The mock flag to test the model.

Methods

__construct()  : mixed
Creates a new Model instance.
__toString()  : string
Returns a String representation of the object.
fetch()  : mixed
fetchAll()  : array<string|int, mixed>
fetchColumn()  : mixed
initializeDebug()  : static
Initialize the debug flag.
initializeMock()  : static
Initialize the mock flag.
isDebug()  : bool
Check if debug mode is active.
isMock()  : bool
Check if mock mode is active.

Constants

DEBUG

The 'debug' parameter constant.

public mixed DEBUG = 'debug'

MOCK

The 'mock' parameter constant.

public mixed MOCK = 'mock'

Properties

$container

The DI container reference.

public Container $container

$debug

Indicates if use the debug mode.

public bool $debug = false

$mock

The mock flag to test the model.

public bool $mock = false

Methods

__construct()

Creates a new Model instance.

public __construct(Container $container[, LoggerInterface|string|null, mock: bool|null} $init = [] ]) : mixed
Parameters
$container : Container

The DI container to retrieve services like PDO and logger.

$init : LoggerInterface|string|null, mock: bool|null} = []

Optional initialization array with keys:

  • debug : Indicates if the debug mode is active (Default false).
  • logger : The optional PSR3 LoggerInterface reference or the name of the reference in the DI Container.
  • mock : Indicates if the model use a mock process (Default false).
Tags
throws
ContainerExceptionInterface

If container service retrieval fails.

throws
NotFoundExceptionInterface

If container service not found.

__toString()

Returns a String representation of the object.

public __toString() : string
Tags
throws
ReflectionException
Return values
string

A string representation of the object.

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'

initializeDebug()

Initialize the debug flag.

public initializeDebug([array<string|int, mixed> $init = [] ][, bool $defaultValue = false ]) : static

This method sets the $debug property using the value provided in the $init array, or falls back to the current $debug value. If the value in $init is not a boolean, the provided $defaultValue is used instead.

Parameters
$init : array<string|int, mixed> = []

Optional initialization array.

$defaultValue : bool = false

Default value to use if the init value is not a boolean.

Return values
static

Returns the current instance for chaining.

initializeMock()

Initialize the mock flag.

public initializeMock([array<string|int, mixed> $init = [] ][, bool $defaultValue = false ]) : static

This method sets the $mock property using the value provided in the $init array, or falls back to the current $mock value. If the value in $init is not a boolean, the provided $defaultValue is used instead.

Parameters
$init : array<string|int, mixed> = []

Optional initialization array.

$defaultValue : bool = false

Default value to use if the init value is not a boolean.

Return values
static

Returns the current instance for chaining.

isDebug()

Check if debug mode is active.

public isDebug([array<string|int, mixed> $init = [] ]) : bool

This method returns the boolean value of the debug flag. If a value is provided in the $init array, it is used; otherwise the current $debug property is used. Non-boolean values in $init are treated as false.

Parameters
$init : array<string|int, mixed> = []

Optional array containing a debug value.

Return values
bool

True if debug mode is active, false otherwise.

isMock()

Check if mock mode is active.

public isMock([array<string|int, mixed> $init = [] ]) : bool

Mock mode is only active if debug mode is active as well. If a value is provided in the $init array, it is used; otherwise the current $mock property is used. Non-boolean values in $init are treated as false.

Parameters
$init : array<string|int, mixed> = []

Optional array containing a mock value.

Return values
bool

True if mock mode is active, false otherwise.


        
On this page

Search results