Documentation

PdbModelTrait

This implements basic methods for {@see PdbModelInterface}.

  • The application is responsible for implementing getConnection().
  • Each concrete model should implement their own getTableName().

For example:

// app/Model.php
abstract class Model implements PdbModelInterface
{
   use PdbModelTrait;

   public static function getConnection(): Pdb
   {
      return MyApp::getPdb();
   }
}

// app/Models/User.php
class User extends Model
{
   public static function getTableName(): string
   {
      return 'users';
   }
}

Table of Contents

Properties

$id  : int

Methods

delete()  : bool
Delete this model.
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.
getConnection()  : Pdb
The connection used queries in this model.
getFieldDefaults()  : array<string|int, mixed>
Get a list of default values for the database record.
getSaveData()  : array<string|int, mixed>
Data to be inserted or updated.
getTableName()  : string
The table name for this model, non-prefixed.
populate()  : void
Populate a model with a DB row.
populateDefaults()  : void
Loads default values from database table schema.
save()  : bool
Save this model.
_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.
typeCastValue()  : void
Convert a property value to it's appropriate type.

Properties

Methods

find()

Create a query for this model.

public static find([array<string|int, mixed> $conditions = [] ]) : PdbModelQuery
Parameters
$conditions : array<string|int, mixed> = []
Return values
PdbModelQuery

findAll()

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
static

findOrCreate()

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
see

{PdbQuery::find()}

Return values
static

getConnection()

The connection used queries in this model.

public abstract static getConnection() : Pdb
Return values
Pdb

getFieldDefaults()

Get a list of default values for the database record.

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

getSaveData()

Data to be inserted or updated.

public getSaveData() : array<string|int, mixed>

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
string

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.

_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
throws
RowMissingException

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|null

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
On this page

Search results