Sledgehammer Framework

ActiveRecord extends Base
in package
Uses EventEmitter

An ActiveRecord frontend for the Repository.

Table of Contents

$_model  : string
The model in the repository, usually the same as the classname.
$_repository  : string|Repository
The repository this instance belongs to.
$_state  : enum
Persistance state: loaded: object was loaded from the persistance layer.
$events  : array<string|int, mixed>
The events/listeners.
$__kvo  : array<string|int, mixed>
Storage array for the properties with KVO (Key Value Observer) listeners.
__call()  : mixed
Report that the $method doesn't exist.
__callStatic()  : mixed
Report that the $method doesn't exist.
__construct()  : mixed
Use the repostory or static functions to create an instance.
__get()  : mixed
Report that $property doesn't exist.
__set()  : mixed
Report that $property doesn't exist and set the property to the given $value.
__toString()  : string
The object is used as an string.
all()  : Collection|ActiveRecord
Retrieve a collection.
create()  : ActiveRecord
Create a new instance.
delete()  : mixed
Delete the instance from the persistance layer.
getChanges()  : array<string|int, mixed>
Retrieve unsaved changes.
hasEvent()  : bool
Check id the $event is a registered event.
off()  : mixed
Remove a callback from an event.
on()  : string
Add a callback for an event.
one()  : ActiveRecord
Find an instance based on critera.
save()  : mixed
Write the state to the persistance layer.
trigger()  : mixed
Trigger an event.
_getModel()  : string
Detect the modelname.
_getRepostory()  : Repository
Detect the repository.
onCreate()  : mixed
Handle the 'create' event.
onDeleted()  : mixed
Handle the 'deleted' event.
onLoad()  : mixed
Handle the 'load' event.
onSaved()  : mixed
Handle the 'saved' event.

Properties

$_model

The model in the repository, usually the same as the classname.

protected string $_model

$_state

Persistance state: loaded: object was loaded from the persistance layer.

protected enum $_state = 'unconstructed'

new: object created, but not written to the persistance layer. saved: object has been saved to the persistance layer. deleted: object is deleted from the persistance layer.

$events

The events/listeners.

protected array<string|int, mixed> $events = [ 'create' => [], // When a new instance is created (with initial data), but commited to the backend. 'load' => [], // After the data from the backend is injected into the ActiveRecord. 'saving' => [], // Before the data is sent to the backend 'saved' => [], // After the data is sent to the backend 'deleting' => [], // Before the delete operation is sent to the backend 'deleted' => [], ]

$__kvo

Storage array for the properties with KVO (Key Value Observer) listeners.

private array<string|int, mixed> $__kvo = []

Methods

__call()

Report that the $method doesn't exist.

public __call(string $method, array<string|int, mixed> $arguments) : mixed
Parameters
$method : string
$arguments : array<string|int, mixed>
Return values
mixed

__callStatic()

Report that the $method doesn't exist.

public static __callStatic(string $method, array<string|int, mixed> $arguments) : mixed
Parameters
$method : string
$arguments : array<string|int, mixed>
Return values
mixed

__construct()

Use the repostory or static functions to create an instance.

public __construct() : mixed
Tags
throws
Exception
Return values
mixed

__get()

Report that $property doesn't exist.

public __get(mixed $property) : mixed
Parameters
$property : mixed
Return values
mixed

__set()

Report that $property doesn't exist and set the property to the given $value.

public __set(mixed $property, mixed $value) : mixed
Parameters
$property : mixed
$value : mixed
Return values
mixed

__toString()

The object is used as an string.

public __toString() : string
Return values
string

all()

Retrieve a collection.

public static all([array<string|int, mixed> $options = [] ]) : Collection|ActiveRecord
Parameters
$options : array<string|int, mixed> = []

array( 'repository' => string (optional) Overwrite the repository. 'model' => string (optional) Overwrite the model name. )

Return values
Collection|ActiveRecord

create()

Create a new instance.

public static create([array<string|int, mixed> $values = [] ][, array<string|int, mixed> $options = [] ]) : ActiveRecord
Parameters
$values : array<string|int, mixed> = []

(optional) Overwrite the default values.

$options : array<string|int, mixed> = []

array( 'repository' => string (optional) Overwrite the repository. 'model' => string (optional) Overwrite the model name. )

Return values
ActiveRecord

delete()

Delete the instance from the persistance layer.

public delete() : mixed
Return values
mixed

getChanges()

Retrieve unsaved changes.

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

hasEvent()

Check id the $event is a registered event.

public hasEvent(string $event) : bool
Parameters
$event : string
Return values
bool

off()

Remove a callback from an event.

public off(string $event, string $identifier) : mixed
Parameters
$event : string
$identifier : string
Return values
mixed

on()

Add a callback for an event.

public on(string $event, Closure|string|array<string|int, mixed> $callback) : string
Parameters
$event : string
$callback : Closure|string|array<string|int, mixed>
Return values
string

identifier

one()

Find an instance based on critera.

public static one(int|string|array<string|int, mixed> $conditions[, mixed $allowNone = false ][, array<string|int, mixed> $options = [] ]) : ActiveRecord

Usage: ActiveRecordSubclass::one(1) ActiveRecordSubclass::one(array('id' => 2));

When the critera matches more than 1 instance an exception is thrown.

Parameters
$conditions : int|string|array<string|int, mixed>
$allowNone : mixed = false
$options : array<string|int, mixed> = []

array( 'repository' => string (optional) Overwrite the repository. 'model' => string (optional) Overwrite the model name. )

Return values
ActiveRecord

save()

Write the state to the persistance layer.

public save([mixed $options = [] ]) : mixed
Parameters
$options : mixed = []
Tags
throws
Exception
Return values
mixed

trigger()

Trigger an event.

public trigger(string $event, stdClass $sender[, mixed $args = null ]) : mixed
Parameters
$event : string
$sender : stdClass
$args : mixed = null

(optional)

Return values
mixed

_getModel()

Detect the modelname.

protected static _getModel([array<string|int, mixed> $options = [] ]) : string

Checks the $options array. Checks if the $_model property is defined in the subclass. Or defaults to the classname.

Parameters
$options : array<string|int, mixed> = []
Return values
string

_getRepostory()

Detect the repository.

protected static _getRepostory([array<string|int, mixed> $options = [] ]) : Repository

Checks the $options array. Checks if the $_repository property is defined in the subclass. Or uses the default repository.

Parameters
$options : array<string|int, mixed> = []
Return values
Repository

onCreate()

Handle the 'create' event.

protected onCreate(object $sender, array<string|int, mixed> $options) : mixed
Parameters
$sender : object
$options : array<string|int, mixed>
Return values
mixed

onDeleted()

Handle the 'deleted' event.

protected onDeleted() : mixed
Return values
mixed

onLoad()

Handle the 'load' event.

protected onLoad(object $sender, array<string|int, mixed> $options) : mixed
Parameters
$sender : object
$options : array<string|int, mixed>
Return values
mixed

onSaved()

Handle the 'saved' event.

protected onSaved() : mixed
Return values
mixed

Search results