Documentation

MenuGroupModel extends Model
in package

Base model class

Table of Contents

Properties

$date_added  : string|null
$date_modified  : string|null
$id  : int
$page_id  : int
$position  : int
$subsite_id  : int
$_cache  : mixed
$_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()  : ModelQuery<string|int, static>
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.
getValidator()  : RulesValidatorInterface
Get the validator for this model.
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.
rules()  : array<string|int, mixed>
save()  : bool
Save this model.
toArray()  : array<string|int, mixed>
Convert this object to an array.
update()  : void
valid()  : array<string|int, mixed>|true
Validate this scenario (or default).
validate()  : void
Validate this scenario (or default).
_afterSave()  : mixed
_beforeSave()  : void
Performed before all save actions.
_getEncrypt()  : EncryptInterface
Get the encrypt helper for this model.
_internalSave()  : mixed
clearCache()  : void
Trash this cache.
getCachedDate()  : DateTimeInterface
Get a date object version of the 'property' field.
getCachedIterable()  : array<string|int, mixed>
Cache an iterable result as an array.
getCachedResult()  : mixed
Cache the result of a function against a hash of the input.
getCachedValue()  : mixed
Cache the result of a function.
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

Methods

__construct()

public __construct([iterable<string|int, mixed> $config = [] ]) : mixed
Parameters
$config : iterable<string|int, mixed> = []

__serialize()

public __serialize() : array<string|int, mixed>
Tags
inheritdoc
Return values
array<string|int, mixed>

__unserialize()

public __unserialize(array<string|int, mixed> $serialized) : void
Parameters
$serialized : array<string|int, mixed>
Tags
inheritdoc

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
see
ArrayableFields
ArrayableTrait
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
see
ArrayableFields
ArrayableTrait
Return values
array<string|int, callable|bool>

find()

Create a query for this model.

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

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 static getConnection() : Pdb
Tags
inheritdoc
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>

getIterator()

public getIterator() : Traversable
Tags
inheritdoc
Return values
Traversable

getSaveData()

Data to be inserted or updated.

public getSaveData() : array<string|int, mixed>
Tags
inheritdoc
Return values
array<string|int, mixed> —

[ column => value ]

getTableName()

The table name for this model, non-prefixed.

public static getTableName() : string
Return values
string

getValidator()

Get the validator for this model.

public getValidator() : RulesValidatorInterface

This is configured in the 'model' config, unless overridden by the model itself.

Return values
RulesValidatorInterface

jsonSerialize()

public jsonSerialize() : mixed
Tags
inheritdoc

offsetExists()

public offsetExists(mixed $offset) : bool
Parameters
$offset : mixed
Return values
bool

offsetGet()

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.

rules()

public rules([string|null $scenario = null ]) : array<string|int, mixed>
Parameters
$scenario : string|null = null
Tags
inheritdoc
Return values
array<string|int, mixed>

save()

Save this model.

public save([bool|string $validate = true ]) : bool
Parameters
$validate : bool|string = true
Tags
throws
ValidationException
PDOException
Return values
bool

toArray()

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>

valid()

Validate this scenario (or default).

public valid([string|null $scenario = null ]) : array<string|int, mixed>|true
Parameters
$scenario : string|null = null

null for default.

Tags
throws
Exception
Return values
array<string|int, mixed>|true —

True if valid, errors array if invalid.

validate()

Validate this scenario (or default).

public validate([string|null $scenario = null ]) : void

This will throw if invalid or pass silently if valid.

Parameters
$scenario : string|null = null

null for default.

Tags
throws
Exception
ValidationException

_afterSave()

protected _afterSave(array<string|int, mixed> $data) : mixed
Parameters
$data : array<string|int, mixed>
Tags
inheritdoc

_beforeSave()

Performed before all save actions.

protected _beforeSave() : void

_getEncrypt()

Get the encrypt helper for this model.

protected _getEncrypt() : EncryptInterface

Default expects a Kohana config entry of encryption.{table_name}. Will only be fired if the table has an encrypt column.

Tags
throws
Exception
Return values
EncryptInterface

_internalSave()

protected _internalSave(array<string|int, mixed> &$data) : mixed
Parameters
$data : array<string|int, mixed>
Tags
inheritdoc

clearCache()

Trash this cache.

protected clearCache([string|null $key = null ]) : void

Maybe you've mutated something. Go on. Trash it.

Provide the key parameter to selectively delete a cached item.

Recommended placements:

  • __clone()
  • DataObject::update()
Parameters
$key : string|null = null

getCachedDate()

Get a date object version of the 'property' field.

protected getCachedDate(string $property) : DateTimeInterface
Parameters
$property : string
Return values
DateTimeInterface

getCachedIterable()

Cache an iterable result as an array.

protected getCachedIterable(string $id, callable|iterable<string|int, mixed> $fn) : array<string|int, mixed>

More useful than you might think. This takes anything you yield and dumps it in a cached array, and also returns it.

Example:

function getItems(): array
{
    return $this->getCachedIterable('items', function() {
        yield 'key1' => 'one';
        yield 'key2' => 'two';
    });
}
Parameters
$id : string
$fn : callable|iterable<string|int, mixed>

() => iterable

Return values
array<string|int, mixed>

getCachedResult()

Cache the result of a function against a hash of the input.

protected getCachedResult(array<string|int, mixed> $inputs, mixed $fn) : mixed

This is largely identical to getCachedValue() but a bit more flexible.

Your inputs must be serializable.

Parameters
$inputs : array<string|int, mixed>
$fn : mixed

(...$inputs) => mixed

getCachedValue()

Cache the result of a function.

protected getCachedValue(string $id, callable $fn) : mixed

Use this with caution plz.

Parameters
$id : string
$fn : callable

() => mixed

Return values
mixed —

function result

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

getSerializedProperties()

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

Search results