Documentation

DbModel extends Model
in package

AbstractYes

Base Active Record-like model for DB-backed entities.

Subclasses must provide tableName() and tableKeys().

Table of Contents

Constants

RULE_EMAIL  : mixed = 'email'
RULE_MATCH  : mixed = 'match'
RULE_MAX  : mixed = 'max'
RULE_MIN  : mixed = 'min'
RULE_REQUIRED  : mixed = 'required'
RULE_UNIQUE  : mixed = 'unique'

Properties

$errors  : array<string|int, mixed>

Methods

addError()  : mixed
attributes()  : mixed
attributesTypes()  : array<string|int, mixed>
Returns an array of column names mapped to their respective database data types.
errorMessage()  : mixed
errorMessages()  : mixed
findOne()  : static|false
Fetch a single record by conditions and map it to the current subclass.
form()  : array<string|int, mixed>
getFirstError()  : mixed
getInputInnerAttribute()  : string|false
getInputInnerAttributes()  : string|false
getInputInnerType()  : string|false
getInputOuterAttribute()  : string|false
getInputOuterAttributes()  : string|false
getInputOuterType()  : string|false
getInputTargetAttribute()  : string|false
getInputTargetAttributes()  : string|false
getInputTargetType()  : string|false
getLabel()  : mixed
getOptionSelects()  : array<string|int, mixed>|false
getProperty()  : string|false
hasError()  : mixed
labels()  : mixed
loadData()  : mixed
prepare()  : PDOStatement
Prepare a SQL statement via the application's PDO connection.
primaryKey()  : string
Returns the primary key column name.
property()  : string
rules()  : mixed
save()  : bool
Insert the current model instance into the database using its attributes.
tableName()  : string
Name of the database table for the model.
validate()  : mixed
addErrorByRule()  : mixed
bindDataToStaticClass()  : static
Binds data to a static class instance by mapping array data to object properties
bindValuesToKeys()  : array<string|int, mixed>
Binds values from class properties to their corresponding keys and applies type casting

Constants

RULE_EMAIL

public mixed RULE_EMAIL = 'email'

RULE_MATCH

public mixed RULE_MATCH = 'match'

RULE_MAX

public mixed RULE_MAX = 'max'

RULE_MIN

public mixed RULE_MIN = 'min'

RULE_REQUIRED

public mixed RULE_REQUIRED = 'required'

RULE_UNIQUE

public mixed RULE_UNIQUE = 'unique'

Properties

$errors

public array<string|int, mixed> $errors = []

Methods

addError()

public addError(string $attribute, string $message) : mixed
Parameters
$attribute : string
$message : string

attributes()

public static attributes() : mixed

attributesTypes()

Returns an array of column names mapped to their respective database data types.

public abstract static attributesTypes() : array<string|int, mixed>

For example, if a column is a datetime, the value should be 'datetime'. If the column is a boolean, the value should be 'boolean'.

Return values
array<string|int, mixed>

errorMessage()

public errorMessage(mixed $rule) : mixed
Parameters
$rule : mixed

errorMessages()

public errorMessages() : mixed

findOne()

Fetch a single record by conditions and map it to the current subclass.

public static findOne(array<string|int, mixed> $where) : static|false
Parameters
$where : array<string|int, mixed>

Associative array of column => value filters.

Return values
static|false

Instance of the model or null if not found.

form()

public form() : array<string|int, mixed>
Return values
array<string|int, mixed>

getFirstError()

public getFirstError(mixed $attribute) : mixed
Parameters
$attribute : mixed

getInputInnerAttribute()

public getInputInnerAttribute(string $property) : string|false
Parameters
$property : string
Return values
string|false

getInputInnerAttributes()

public getInputInnerAttributes() : string|false
Return values
string|false

getInputInnerType()

public getInputInnerType() : string|false
Return values
string|false

getInputOuterAttribute()

public getInputOuterAttribute(string $property) : string|false
Parameters
$property : string
Return values
string|false

getInputOuterAttributes()

public getInputOuterAttributes() : string|false
Return values
string|false

getInputOuterType()

public getInputOuterType() : string|false
Return values
string|false

getInputTargetAttribute()

public getInputTargetAttribute(string $property) : string|false
Parameters
$property : string
Return values
string|false

getInputTargetAttributes()

public getInputTargetAttributes() : string|false
Return values
string|false

getInputTargetType()

public getInputTargetType() : string|false
Return values
string|false

getLabel()

public getLabel(mixed $attribute) : mixed
Parameters
$attribute : mixed

getOptionSelects()

public getOptionSelects(string $attribute) : array<string|int, mixed>|false
Parameters
$attribute : string
Return values
array<string|int, mixed>|false

getProperty()

public getProperty(string $attribute, string $property) : string|false
Parameters
$attribute : string
$property : string
Return values
string|false

hasError()

public hasError(mixed $attribute) : mixed
Parameters
$attribute : mixed

labels()

public labels() : mixed

loadData()

public loadData(mixed $data) : mixed
Parameters
$data : mixed

prepare()

Prepare a SQL statement via the application's PDO connection.

public static prepare(string $sql) : PDOStatement
Parameters
$sql : string

Raw SQL with named placeholders.

Return values
PDOStatement

primaryKey()

Returns the primary key column name.

public static primaryKey() : string
Return values
string

property()

public property(string $attribute) : string
Parameters
$attribute : string
Return values
string

rules()

public rules() : mixed

save()

Insert the current model instance into the database using its attributes.

public save() : bool

Binds attributes as named parameters and executes the INSERT statement.

Return values
bool

True on successful execution.

tableName()

Name of the database table for the model.

public abstract static tableName() : string
Return values
string

validate()

public validate() : mixed

addErrorByRule()

protected addErrorByRule(string $attribute, string $rule[, mixed $params = [] ]) : mixed
Parameters
$attribute : string
$rule : string
$params : mixed = []

bindDataToStaticClass()

Binds data to a static class instance by mapping array data to object properties

private static bindDataToStaticClass(array<string|int, mixed> $data, array<string|int, mixed>|null $attributesTypes[, bool $autoRestrict = true ]) : static

This private static method takes an associative array of data and maps it to the properties of the current class. It provides flexible data binding with optional type casting and automatic restriction of which properties can be set. The method offers two modes:

  1. Auto-restrict mode (default) - Only allows properties that exist in the class
  2. Non-auto-restrict mode - Allows setting any property, even if it doesn't exist

When autoRestrict is true and data is provided:

  • For each data key-value pair, it attempts to map values based on type information
  • Uses Reflection to inspect class properties and their types
  • Applies automatic type conversion through the AutoMapped::mapValueFromDB() method
  • Respects property-level #[Cast] attributes for specialized casting
Parameters
$data : array<string|int, mixed>

Associative array of data to bind to object properties

$attributesTypes : array<string|int, mixed>|null

Optional array of attribute type mappings for type checking

$autoRestrict : bool = true

If true, only allows setting properties that exist in the class

Return values
static

Returns a new instance of the calling class with bound data

bindValuesToKeys()

Binds values from class properties to their corresponding keys and applies type casting

private static bindValuesToKeys(object $modelClass, array<string|int, mixed> $classProperty, array<string|int, mixed> $attributesTypes) : array<string|int, mixed>

This private static method takes a model instance and maps its properties to a key-value array, applying type conversions based on specified attribute types and property-level cast attributes. It's designed to prepare data for database operations, API responses, or data transfer scenarios.

The method performs several operations:

  1. Processes attributesTypes array to map values with proper type conversion
  2. Uses Reflection to inspect and validate properties before processing
  3. Respects #[Cast] attributes for specialized value conversions
Parameters
$modelClass : object

The model instance containing properties to be processed

$classProperty : array<string|int, mixed>

Associative array of property names and their values

$attributesTypes : array<string|int, mixed>

Array mapping property names to their expected types for conversion

Return values
array<string|int, mixed>

Returns an associative array with property names as keys and converted values

On this page

Search results