Record
extends Collection
in package
implements
PdbModelInterface
uses
PdbModelTrait
This is a lightweight model.
No validation, no caching, no magic fields.
Table of Contents
Interfaces
- PdbModelInterface
- The skeleton structure of a model.
Properties
- $id : int
- $_checksums : DirtyChecksums|null
Methods
- __construct() : mixed
- __serialize() : array<string|int, mixed>
- __unserialize() : void
- delete() : bool
- Delete this model.
- extraFields() : array<string|int, callable>
- Define extra fields that you _might_ want included in the `toArray()` output.
- fields() : array<string|int, callable|bool>
- These are fields for use in `toArray()`.
- find() : PdbModelQuery
- Create a query for this model.
- findAll() : array<string|int, static>
- Find a list of models.
- findOne() : static
- Find one model.
- findOrCreate() : static
- Find one model. Create a default one if not found.
- getChecksums() : DirtyChecksums
- Get a checksums store for this object.
- getConnection() : Pdb
- The connection used queries in this model.
- getFieldDefaults() : array<string|int, mixed>
- Get a list of default values for the database record.
- getIterator() : Traversable
- getSaveData() : array<string|int, mixed>
- Data to be inserted or updated.
- getTableName() : string
- The table name for this model, non-prefixed.
- jsonSerialize() : mixed
- offsetExists() : bool
- offsetGet() : mixed
- offsetSet() : void
- offsetUnset() : void
- populate() : void
- Populate a model with a DB row.
- populateDefaults() : void
- Loads default values from database table schema.
- save() : bool
- Save this model.
- toArray() : array<string|int, mixed>
- Convert this object to an array.
- update() : void
- _afterSave() : void
- Performed after all save actions.
- _beforeSave() : void
- Performed before all save actions.
- _internalSave() : void
- Perform inserts and updates.
- getFieldList() : array<string, PdbColumn>
- Get the field definitions for the table that holds this record
- getFieldType() : string|null
- Test for a field type.
- getSerializedProperties() : array<string|int, mixed>
- Get an array of properties to serialize.
- typeCastValue() : void
- Convert a property value to it's appropriate type.
Properties
$id
public
int
$id
= 0
$_checksums
private
DirtyChecksums|null
$_checksums
Methods
__construct()
public
__construct([iterable<string|int, mixed> $config = [] ]) : mixed
Parameters
- $config : iterable<string|int, mixed> = []
__serialize()
public
__serialize() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>__unserialize()
public
__unserialize(array<string|int, mixed> $serialized) : void
Parameters
- $serialized : array<string|int, mixed>
Tags
delete()
Delete this model.
public
delete() : bool
Tags
Return values
bool —True if the record was deleted.
extraFields()
Define extra fields that you _might_ want included in the `toArray()` output.
public
extraFields() : array<string|int, callable>
These are virtual fields that are not included by default like the
ones defined in fields(). Instead, use the second parameter of
the toArray(null, ['name']) method to specify which extra fields
you want in the output.
A virtual field is defined as a pair like 'name' => callback.
Tags
Return values
array<string|int, callable>fields()
These are fields for use in `toArray()`.
public
fields() : array<string|int, callable|bool>
By default this will include all public fields of the object.
Given 'name' => true a field will appear in the array output. Given
false|null|0|unset|... the field will not be present.
Define a pair like 'name' => callback create a 'virtual' field. This
can override existing properties or create whole new fields.
Note, if not implementing the ArrayableFields interface, this method will only define 'virtual' fields. That said, inheriting the parent fields is safe and will future-proof the code.
// Inheriting fields from the parent:
$fields = parent::fields();
$fields['hide_this_field'] = false;
unset($fields['this_also_works']);
return $fields;
// Declaring explicit fields, ignoring inherited:
return [
'real_field' => true,
'and_this_field' => true,
];
// Adding virtual fields:
$fields = parent::fields();
$fields['virtual_thing'] = [$this, 'getMyVirtualThing'];
$fields['inline_thing'] = function() {
return 'hey look: ' . time();
};
return $fields;
Tags
Return values
array<string|int, callable|bool>find()
Create a query for this model.
public
static find([array<string|int, mixed> $conditions = [] ]) : PdbModelQuery
Parameters
- $conditions : array<string|int, mixed> = []
Return values
PdbModelQueryfindAll()
Find a list of models.
public
static findAll([array<string|int, mixed> $conditions = [] ]) : array<string|int, static>
Parameters
- $conditions : array<string|int, mixed> = []
Return values
array<string|int, static>findOne()
Find one model.
public
static findOne(array<string|int, mixed> $conditions) : static
Parameters
- $conditions : array<string|int, mixed>
Return values
staticfindOrCreate()
Find one model. Create a default one if not found.
public
static findOrCreate(array<string|int, mixed> $conditions[, bool $update = true ]) : static
Parameters
- $conditions : array<string|int, mixed>
- $update : bool = true
-
If true, set 'conditions' as properties on the model.
Tags
Return values
staticgetChecksums()
Get a checksums store for this object.
public
getChecksums() : DirtyChecksums
Call this ASAP in your object lifecycle, i.e. the constructor.
Note this only tracks public properties.
Return values
DirtyChecksumsgetConnection()
The connection used queries in this model.
public
static getConnection() : Pdb
Tags
Return values
PdbgetFieldDefaults()
Get a list of default values for the database record.
public
static getFieldDefaults() : array<string|int, mixed>
Return values
array<string|int, mixed>getIterator()
public
getIterator() : Traversable
Tags
Return values
TraversablegetSaveData()
Data to be inserted or updated.
public
getSaveData() : array<string|int, mixed>
Public, private and protected properties are included. Any properties prefixed with an underscore are ignored.
This is a perfect spot to add generated values like audit rows (date_added, date_modified, uid, etc).
Override this to implement dirty-property behaviour.
Return values
array<string|int, mixed> —[ column => value ]
getTableName()
The table name for this model, non-prefixed.
public
abstract static getTableName() : string
Return values
stringjsonSerialize()
public
jsonSerialize() : mixed
Tags
offsetExists()
public
offsetExists(mixed $offset) : bool
Parameters
- $offset : mixed
Return values
booloffsetGet()
public
offsetGet(mixed $offset) : mixed
Parameters
- $offset : mixed
offsetSet()
public
offsetSet(mixed $offset, mixed $value) : void
Parameters
- $offset : mixed
- $value : mixed
offsetUnset()
public
offsetUnset(mixed $offset) : void
Parameters
- $offset : mixed
populate()
Populate a model with a DB row.
public
static populate(static $instance, array<string|int, mixed> $config) : void
Extend this to implement custom logic, e.g. dirty property behaviour.
Parameters
- $instance : static
- $config : array<string|int, mixed>
populateDefaults()
Loads default values from database table schema.
public
populateDefaults() : void
This will only set defaults values for properties that are null.
save()
Save this model.
public
save() : bool
Tags
Return values
booltoArray()
Convert this object to an array.
public
toArray([array<string|int, string>|null $filter = null ][, array<string|int, string>|null $extra = null ][, bool $nulls = false ]) : array<string|int, mixed>
Specify a 'filter' to restrict the fields included in the output.
Specify an 'extra' to include additional fields if implementing the
ArrayableFields interface. This will add properties that were excluded
from fields() and includes any virtual fields defined in extraFields().
included in fields() and any virtual fields defined in extraFields().
Parameters
- $filter : array<string|int, string>|null = null
- $extra : array<string|int, string>|null = null
- $nulls : bool = false
-
- include null fields in the output
Return values
array<string|int, mixed>update()
public
update(iterable<string|int, mixed> $config) : void
Parameters
- $config : iterable<string|int, mixed>
_afterSave()
Performed after all save actions.
protected
_afterSave(array<string|int, mixed> $data) : void
Parameters
- $data : array<string|int, mixed>
-
as created by getSaveData()
_beforeSave()
Performed before all save actions.
protected
_beforeSave() : void
_internalSave()
Perform inserts and updates.
protected
_internalSave(array<string|int, mixed> &$data) : void
This is wrapped by the save() method with a transaction.
This modifies the 'ID' into the $data array to the
Parameters
- $data : array<string|int, mixed>
-
as created by getSaveData(), this is mutable
Tags
getFieldList()
Get the field definitions for the table that holds this record
protected
static getFieldList() : array<string, PdbColumn>
Return values
array<string, PdbColumn>getFieldType()
Test for a field type.
protected
static getFieldType(string $field_name) : string|null
Parameters
- $field_name : string
Return values
string|nullgetSerializedProperties()
Get an array of properties to serialize.
protected
getSerializedProperties() : array<string|int, mixed>
Override this to modify the behaviour of the serializer.
By default, this returns properties that are:
- public
- not private or protected
- not static
- not implementing
NotSerializable
Return values
array<string|int, mixed> —[string => value]
typeCastValue()
Convert a property value to it's appropriate type.
protected
static typeCastValue(static $instance, string $property, mixed &$value) : void
Supported types:
- array: from JSON-encoded string
- array: from comma-separated SET
- bool: from '0' or '1'
- int: from numeric string
- float: from numeric string
- datetime: from string
- string: from anything else
Parameters
- $instance : static
- $property : string
- $value : mixed