abstract class ORM implements JsonSerializable

ORM.

Constants

PRIMARY_KEY_TYPE_INCREMENTING

Incrementing primary key.

PRIMARY_KEY_TYPE_UUID

UUID primary key.

PRIMARY_KEY_TYPE_CUSTOM

Custom primary key.

PRIMARY_KEY_TYPE_NONE

No primary key.

Properties

protected string $connectionName Connection name to use for the model.
static protected ConnectionManager $connectionManager Connection manager instance.
static protected array $traitHooks ORM query builder hooks.
static protected array $traitCasts Trait casts.
protected string $tableName Table name.
protected string $foreignKeyName Foreign key name.
protected string $primaryKey Primary key.
protected int $primaryKeyType Does this table have an auto increment primary index?
protected bool $isPersisted Has the record been loaded from/saved to a database?
protected array $columns Column values.
protected array $original Original column values.
protected array $including Relations to eager load.
$related Related records.
protected array $cast Columns that should be casted to a specific type.
protected array $assignable Columns that can be set through mass assignment.
protected array $protected Columns and relations that are excluded from the array and json representations of the record.
protected string $dateOutputFormat Date format used when returning array and json representations of the record.

Methods

__construct(array $columns = [], bool $raw = false, bool $whitelist = true, bool $isPersisted = false)

Constructor.

__clone()

Making sure that cloning returns a "fresh copy" of the record.

static 
setConnectionManager(ConnectionManager $connectionManager)

Set the connection manager.

getConnection()

Returns the connection of the model.

bool
isPersisted()

Has the record been loaded from/saved to a database?

synchronize()

Synchronizes the original values with the modified values.

string
getDateFormat()

Gets the date format from the query builder compiler.

registerHooksAndCasts()

Registers hooks and casts.

array
bindHooks(array $hooks)

Binds the hooks to the current instance of "$this".

array
getHooks(string $event)

Returns hooks for the chosen event.

string
getClassShortName(string|null $className = null)

Returns the short name of a class.

string
getTable()

Returns the table name of the model.

string
getPrimaryKey()

Returns the primary key of the table.

int
getPrimaryKeyType()

Returns the primary key type.

mixed
getPrimaryKeyValue()

Returns the primary key value.

string
getForeignKey()

Returns the foreign key of the table.

string
getClass()

Returns the namespaced class name of the model.

setIncludes(array $includes)

Sets the relations to eager load.

array
getIncludes()

Returns the relations to eager load.

setRelated(string $relation, mixed $related)

Sets eagerly loaded related records.

bool
includes(string $relation)

Returns TRUE if the model has included the relationship and FALSE if not.

$this
include(string|array $includes)

Eager loads relations on the model.

array
getRelated()

Returns the related records array.

mixed
cast(string $name, mixed $value)

Cast value to the appropriate type.

setRawColumnValue(string $name, mixed $value)

Sets a raw column value.

setColumnValue(string $name, mixed $value)

Sets a column value.

mixed
getRawColumnValue(string $name)

Gets a raw column value.

mixed
getColumnValue(string $name)

Returns a column value.

bool
isRelation(string $name)

Returns TRUE if it's probable that $name is a relation and FALSE if not.

mixed
getValue(string $name)

Gets a column value or relation.

array
getRawColumnValues()

Returns the columns array.

setColumValues(array $columns, bool $raw)

Sets column values.

$this
assign(array $columns, bool $raw = false, bool $whitelist = true)

Assigns the column values to the model.

__set(string $name, mixed $value)

Set column value using overloading.

mixed
__get(string $name)

Get column value or relation using overloading.

bool
__isset(string $name)

Checks if a column or relation is set using overloading.

__unset(string $name)

Unset column value or relation using overloading.

builder()

Returns a query builder instance.

static ORM
get(mixed $id, array $columns = [])

Returns a record using the value of its primary key.

static ORM
create(array $columns = [], bool $raw = false, bool $whitelist = true)

Creates a new record and returns the model.

hasOne(string $model, string|null $foreignKey = null)

Returns a HasOne relation.

hasOnePolymorphic(string $model, string $polymorphicType)

Returns a HasOnePolymorphic relation.

hasMany(string $model, string|null $foreignKey = null)

Returns a HasMany relation.

hasManyPolymorphic(string $model, string $polymorphicType)

Returns a HasManyPolymorphic relation.

manyToMany(string $model, string|null $foreignKey = null, string|null $junctionTable = null, string|null $junctionKey = null)

Returns a ManyToMany relation.

belongsTo(string $model, string|null $foreignKey = null)

Returns a BelongsTo relation.

belongsToPolymorphic(string $model, string $polymorphicType)

Returns a BelongsToPolymorphic relation.

bool
isModified()

Has the record been modified?

array
getModified()

Returns the modified column values of the record.

mixed
generatePrimaryKey()

Generates a primary key.

insertRecord(Query $query)

Inserts a new record into the database.

bool
updateRecord(Query $query)

Updates an existing record.

bool
save()

Saves the record to the database.

bool
deleteRecord(Query $query)

Deletes a record from the database.

bool
delete()

Deletes a record from the database.

$this
protect(string|array|bool $column)

Excludes the chosen columns and relations from array and json representations of the record.

$this
expose(string|array|bool $column)

Exposes the chosen columns and relations in the array and json representations of the record.

array
toArray()

Returns an array representation of the record.

array
jsonSerialize()

Returns data which can be serialized by json_encode().

string
toJson(int $options = 0)

Returns a json representation of the record.

string
__toString()

Returns a json representation of the record.

mixed
__call(string $name, array $arguments)

Forwards method calls to the query builder.

static mixed
__callStatic(string $name, array $arguments)

Forwards static method calls to the query builder.

Details

at line 205
__construct(array $columns = [], bool $raw = false, bool $whitelist = true, bool $isPersisted = false)

Constructor.

Parameters

array $columns Column values
bool $raw Set raw values?
bool $whitelist Remove columns that are not in the whitelist?
bool $isPersisted Does the record come from a database?

at line 222
__clone()

Making sure that cloning returns a "fresh copy" of the record.

at line 239
static setConnectionManager(ConnectionManager $connectionManager)

Set the connection manager.

Parameters

ConnectionManager $connectionManager Connection manager instance

at line 249
Connection getConnection()

Returns the connection of the model.

Return Value

Connection

at line 264
bool isPersisted()

Has the record been loaded from/saved to a database?

Return Value

bool

at line 272
synchronize()

Synchronizes the original values with the modified values.

at line 282
protected string getDateFormat()

Gets the date format from the query builder compiler.

Return Value

string

at line 297
protected registerHooksAndCasts()

Registers hooks and casts.

at line 327
protected array bindHooks(array $hooks)

Binds the hooks to the current instance of "$this".

Parameters

array $hooks Array of hooks

Return Value

array

at line 345
array getHooks(string $event)

Returns hooks for the chosen event.

Parameters

string $event Event name

Return Value

array

at line 356
protected string getClassShortName(string|null $className = null)

Returns the short name of a class.

Parameters

string|null $className Class name

Return Value

string

at line 375
string getTable()

Returns the table name of the model.

Return Value

string

at line 390
string getPrimaryKey()

Returns the primary key of the table.

Return Value

string

at line 400
int getPrimaryKeyType()

Returns the primary key type.

Return Value

int

at line 410
mixed getPrimaryKeyValue()

Returns the primary key value.

Return Value

mixed

at line 420
string getForeignKey()

Returns the foreign key of the table.

Return Value

string

at line 435
string getClass()

Returns the namespaced class name of the model.

Return Value

string

at line 445
setIncludes(array $includes)

Sets the relations to eager load.

Parameters

array $includes Relations to eager load

at line 455
array getIncludes()

Returns the relations to eager load.

Return Value

array

at line 466
setRelated(string $relation, mixed $related)

Sets eagerly loaded related records.

Parameters

string $relation Relation name
mixed $related Related record(s)

at line 477
bool includes(string $relation)

Returns TRUE if the model has included the relationship and FALSE if not.

Parameters

string $relation Relation name

Return Value

bool

at line 488
$this include(string|array $includes)

Eager loads relations on the model.

Parameters

string|array $includes Relation or array of relations to eager load

Return Value

$this

at line 503
array getRelated()

Returns the related records array.

Return Value

array

at line 515
protected mixed cast(string $name, mixed $value)

Cast value to the appropriate type.

Parameters

string $name Column name
mixed $value Column value

Return Value

mixed

at line 545
setRawColumnValue(string $name, mixed $value)

Sets a raw column value.

Parameters

string $name Column name
mixed $value Column value

at line 556
setColumnValue(string $name, mixed $value)

Sets a column value.

Parameters

string $name Column name
mixed $value Column value

at line 576
mixed getRawColumnValue(string $name)

Gets a raw column value.

Parameters

string $name Column name

Return Value

mixed

at line 587
mixed getColumnValue(string $name)

Returns a column value.

Parameters

string $name Column name

Return Value

mixed

at line 603
protected bool isRelation(string $name)

Returns TRUE if it's probable that $name is a relation and FALSE if not.

Parameters

string $name Relation name

Return Value

bool

at line 614
mixed getValue(string $name)

Gets a column value or relation.

Parameters

string $name Column name

Return Value

mixed

at line 645
array getRawColumnValues()

Returns the columns array.

Return Value

array

at line 656
protected setColumValues(array $columns, bool $raw)

Sets column values.

Parameters

array $columns Column values
bool $raw Set raw values?

at line 689
$this assign(array $columns, bool $raw = false, bool $whitelist = true)

Assigns the column values to the model.

Parameters

array $columns Column values
bool $raw Set raw values?
bool $whitelist Remove columns that are not in the whitelist?

Return Value

$this

at line 718
__set(string $name, mixed $value)

Set column value using overloading.

Parameters

string $name Column name
mixed $value Column value

at line 729
mixed __get(string $name)

Get column value or relation using overloading.

Parameters

string $name Column name

Return Value

mixed

at line 740
bool __isset(string $name)

Checks if a column or relation is set using overloading.

Parameters

string $name Column or relation name

Return Value

bool

at line 755
__unset(string $name)

Unset column value or relation using overloading.

Parameters

string $name Column name

at line 765
Query builder()

Returns a query builder instance.

Return Value

Query

at line 777
static ORM get(mixed $id, array $columns = [])

Returns a record using the value of its primary key.

Parameters

mixed $id Primary key
array $columns Columns to select

Return Value

ORM

at line 790
static ORM create(array $columns = [], bool $raw = false, bool $whitelist = true)

Creates a new record and returns the model.

Parameters

array $columns Column values
bool $raw Set raw values?
bool $whitelist Remove columns that are not in the whitelist?

Return Value

ORM

at line 806
protected HasOne hasOne(string $model, string|null $foreignKey = null)

Returns a HasOne relation.

Parameters

string $model Related model
string|null $foreignKey Foreign key name

Return Value

HasOne

at line 820
protected HasOnePolymorphic hasOnePolymorphic(string $model, string $polymorphicType)

Returns a HasOnePolymorphic relation.

Parameters

string $model Related model
string $polymorphicType Polymorphic type

Return Value

HasOnePolymorphic

at line 834
protected HasMany hasMany(string $model, string|null $foreignKey = null)

Returns a HasMany relation.

Parameters

string $model Related model
string|null $foreignKey Foreign key name

Return Value

HasMany

at line 848
protected HasManyPolymorphic hasManyPolymorphic(string $model, string $polymorphicType)

Returns a HasManyPolymorphic relation.

Parameters

string $model Related model
string $polymorphicType Polymorphic type

Return Value

HasManyPolymorphic

at line 864
protected ManyToMany manyToMany(string $model, string|null $foreignKey = null, string|null $junctionTable = null, string|null $junctionKey = null)

Returns a ManyToMany relation.

Parameters

string $model Related model
string|null $foreignKey Foreign key name
string|null $junctionTable Junction table name
string|null $junctionKey Junction key name

Return Value

ManyToMany

at line 878
protected BelongsTo belongsTo(string $model, string|null $foreignKey = null)

Returns a BelongsTo relation.

Parameters

string $model Related model
string|null $foreignKey Foreign key name

Return Value

BelongsTo

at line 892
protected BelongsToPolymorphic belongsToPolymorphic(string $model, string $polymorphicType)

Returns a BelongsToPolymorphic relation.

Parameters

string $model Related model
string $polymorphicType Polymorphic type

Return Value

BelongsToPolymorphic

at line 904
bool isModified()

Has the record been modified?

Return Value

bool

at line 914
array getModified()

Returns the modified column values of the record.

Return Value

array

at line 934
protected mixed generatePrimaryKey()

Generates a primary key.

Return Value

mixed

at line 944
protected insertRecord(Query $query)

Inserts a new record into the database.

Parameters

Query $query Query builder

at line 972
protected bool updateRecord(Query $query)

Updates an existing record.

Parameters

Query $query Query builder

Return Value

bool

at line 984
bool save()

Saves the record to the database.

Return Value

bool

at line 1019
protected bool deleteRecord(Query $query)

Deletes a record from the database.

Parameters

Query $query Query builder

Return Value

bool

at line 1029
bool delete()

Deletes a record from the database.

Return Value

bool

at line 1055
$this protect(string|array|bool $column)

Excludes the chosen columns and relations from array and json representations of the record.

You expose all fields by passing FALSE.

Parameters

string|array|bool $column Column or relation to hide from the

Return Value

$this

at line 1069
$this expose(string|array|bool $column)

Exposes the chosen columns and relations in the array and json representations of the record.

You can expose all fields by passing TRUE.

Parameters

string|array|bool $column Column or relation to hide from the

Return Value

$this

at line 1081
array toArray()

Returns an array representation of the record.

Return Value

array

at line 1128
array jsonSerialize()

Returns data which can be serialized by json_encode().

Return Value

array

at line 1139
string toJson(int $options = 0)

Returns a json representation of the record.

Parameters

int $options JSON encode options

Return Value

string

at line 1149
string __toString()

Returns a json representation of the record.

Return Value

string

at line 1161
mixed __call(string $name, array $arguments)

Forwards method calls to the query builder.

Parameters

string $name Method name
array $arguments Method arguments

Return Value

mixed

at line 1173
static mixed __callStatic(string $name, array $arguments)

Forwards static method calls to the query builder.

Parameters

string $name Method name
array $arguments Method arguments

Return Value

mixed