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
initializeMock()  : bool
Initialize the mock flag.
isMock()  : bool
Indicates if the document use the mock mode.

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'

initializeMock()

Initialize the mock flag.

public initializeMock([array<string|int, mixed> $init = [] ]) : bool
Parameters
$init : array<string|int, mixed> = []
Return values
bool

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
bool

        
On this page

Search results