tflori/orm

just another orm...

View the Project on GitHub tflori/orm

Build Status Test Coverage Maintainability Latest Stable Version Total Downloads

» API Reference » ORM\Entity

ORM\Entity

Table of Contents

Abstract

Implements: Serializable

Definition of an entity

The instance of an entity represents a row of the table and the statics variables and methods describe the database table.

This is the main part where your configuration efforts go. The following properties and methods are well documented in the manual under https://tflori.github.io/orm/entityDefinition.html.

See Also:

Constants

Name Value
OPT_RELATION_CLASS 'class'
OPT_RELATION_CARDINALITY 'cardinality'
OPT_RELATION_REFERENCE 'reference'
OPT_RELATION_OPPONENT 'opponent'
OPT_RELATION_TABLE 'table'

Methods

ORM\Entity::__construct

final public function __construct(
    array $data = [], 
    \ORM\EntityManager $entityManager = null, 
    bool $fromDatabase = false, 
)
Constructor

It calls ::onInit() after initializing $data and $originalData.

Parameters
Parameter Type Description
$data array The current data
$entityManager ORM\EntityManager The EntityManager that created this entity
$fromDatabase bool Whether or not the data comes from database

ORM\Entity::__construct

↑ top


ORM\Entity::__get

public function __get(string $attribute): mixed|null
Returns mixed|null  
Parameters
Parameter Type Description
$attribute string  

See Also:

ORM\Entity::__get

↑ top


ORM\Entity::__isset

public function __isset(mixed $attribute): bool
Check if a column is defined
Returns bool  
Parameters
Parameter Type Description
$attribute mixed  

ORM\Entity::__isset

↑ top


ORM\Entity::__serialize

public function __serialize(): mixed
Returns mixed  

ORM\Entity::__serialize

↑ top


ORM\Entity::__set

public function __set(string $attribute, mixed $value): mixed
Returns mixed  
Parameters
Parameter Type Description
$attribute string The variable to change
$value mixed The value to store

See Also:

ORM\Entity::__set

↑ top


ORM\Entity::__unserialize

public function __unserialize(mixed $data): mixed
Returns mixed  
Parameters
Parameter Type Description
$data mixed  

ORM\Entity::__unserialize

↑ top


ORM\Entity::addRelated

public function addRelated(string $relation, \ORM\Entity[] $entities): mixed
Add relations for $relation to $entities

This method is only for many-to-many relations.

This method does not take care about already existing relations and will fail hard.

Returns mixed  
Parameters
Parameter Type Description
$relation string  
$entities ORM\Entity[]  

ORM\Entity::addRelated

↑ top


ORM\Entity::bootIfNotBooted

public static function bootIfNotBooted(): mixed
Boot the class if it not already booted
Static   This method is static.
Returns mixed  

ORM\Entity::bootIfNotBooted

↑ top


ORM\Entity::delete

public function delete(): $this
Delete the entity

You might want to implement soft deletes by overriding this method.

Returns $this  

ORM\Entity::delete

↑ top


ORM\Entity::deleteRelated

public function deleteRelated(string $relation, \ORM\Entity[] $entities): mixed
Delete relations for $relation to $entities

This method is only for many-to-many relations.

Returns mixed  
Parameters
Parameter Type Description
$relation string  
$entities ORM\Entity[]  

ORM\Entity::deleteRelated

↑ top


ORM\Entity::describe

public static function describe(): \ORM\Dbal\Table|\ORM\Dbal\Column[]
Get a description for this table.
Static   This method is static.
Returns ORM\Dbal\Table|ORM\Dbal\Column[]  

ORM\Entity::describe

↑ top


ORM\Entity::detachObserver

public static function detachObserver(
    \ORM\Observer\AbstractObserver $observer, 
): mixed
Stop observing the class by $observer
Static   This method is static.
Returns mixed  
Parameters
Parameter Type Description
$observer ORM\Observer\AbstractObserver  

See Also:

ORM\Entity::detachObserver

↑ top


ORM\Entity::disableValidator

public static function disableValidator(bool $disable = true): mixed
Disable validator
Static   This method is static.
Returns mixed  
Parameters
Parameter Type Description
$disable bool  

ORM\Entity::disableValidator

↑ top


ORM\Entity::enableValidator

public static function enableValidator(bool $enable = true): mixed
Enable validator
Static   This method is static.
Returns mixed  
Parameters
Parameter Type Description
$enable bool  

ORM\Entity::enableValidator

↑ top


ORM\Entity::exists

public function exists(): bool
Returns whether or not the entity exists in database
Returns bool  

ORM\Entity::exists

↑ top


ORM\Entity::fetch

public function fetch(
    string $relation, 
    bool $getAll = false, 
): \ORM\Entity|\ORM\Entity[]|\ORM\EntityFetcher

For relations with cardinality many it returns an EntityFetcher. Otherwise it returns the entity.

It will throw an error for non owner when the key is incomplete.

Returns ORM\Entity|ORM\Entity[]|ORM\EntityFetcher  
Parameters
Parameter Type Description
$relation string The relation to fetch
$getAll bool  

ORM\Entity::fetch

↑ top


ORM\Entity::fill

public function fill(
    array $data, 
    bool $ignoreUnknown = false, 
    bool $checkMissing = false, 
): mixed
Fill the entity with $data

When $checkMissing is set to true it also proves that the absent columns are nullable.

Returns mixed  
Throws ORM\Exception\UnknownColumn  
Throws ORM\Dbal\Error  
Parameters
Parameter Type Description
$data array  
$ignoreUnknown bool  
$checkMissing bool  

ORM\Entity::fill

↑ top


ORM\Entity::getAttribute

public function getAttribute(string $attribute): mixed|null
Get the value from $attribute

If there is a custom getter this method get called instead.

Returns mixed|null  
Parameters
Parameter Type Description
$attribute string The variable to get

See Also:

ORM\Entity::getAttribute

↑ top


ORM\Entity::getAttributeName

public static function getAttributeName(string $column): string
Get the column name of $attribute

The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced to $namingSchemeColumn.

ATTENTION: If your overwrite this method remember that getColumnName(getColumnName($name)) have to be exactly the same as getColumnName($name).

Static   This method is static.
Returns string  
Parameters
Parameter Type Description
$column string  

ORM\Entity::getAttributeName

↑ top


ORM\Entity::getColumnName

public static function getColumnName(string $attribute): string
Get the column name of $attribute

The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced to $namingSchemeColumn.

ATTENTION: If your overwrite this method remember that getColumnName(getColumnName($name)) have to be exactly the same as getColumnName($name).

Static   This method is static.
Returns string  
Parameters
Parameter Type Description
$attribute string  

ORM\Entity::getColumnName

↑ top


ORM\Entity::getDirty

public function getDirty(): array
Get an array of attributes that changed

This method works on application level. Meaning it is showing additional attributes defined in ::$includedAttributes and and hiding ::$excludedAttributes.

Returns array  

ORM\Entity::getDirty

↑ top


ORM\Entity::getNamingSchemeColumn

public static function getNamingSchemeColumn(): string

Warning: this method is deprecated. | Static | | This method is static. | | Returns | string | |

ORM\Entity::getNamingSchemeColumn

↑ top


ORM\Entity::getNamingSchemeMethods

public static function getNamingSchemeMethods(): string

Warning: this method is deprecated. | Static | | This method is static. | | Returns | string | |

ORM\Entity::getNamingSchemeMethods

↑ top


ORM\Entity::getNamingSchemeTable

public static function getNamingSchemeTable(): string

Warning: this method is deprecated. | Static | | This method is static. | | Returns | string | |

ORM\Entity::getNamingSchemeTable

↑ top


ORM\Entity::getPrimaryKey

public function getPrimaryKey(): array
Get the primary key
Returns array  
Throws ORM\Exception\IncompletePrimaryKey  

ORM\Entity::getPrimaryKey

↑ top


ORM\Entity::getPrimaryKeyVars

public static function getPrimaryKeyVars(): array
Get the primary key vars

The primary key can consist of multiple columns. You should configure the vars that are translated to these columns.

Static   This method is static.
Returns array  

ORM\Entity::getPrimaryKeyVars

↑ top


ORM\Entity::getRelated

public function getRelated(string $relation, bool $refresh = false): mixed

The difference between getRelated and fetch is that getRelated stores the fetched entities. To refresh set $refresh to true.

Returns mixed  
Parameters
Parameter Type Description
$relation string  
$refresh bool  

ORM\Entity::getRelated

↑ top


ORM\Entity::getRelation

public static function getRelation(string $name): \ORM\Relation
Get the definition for $relation

It normalize the short definition form and create a Relation object from it.

Static   This method is static.
Returns ORM\Relation  
Throws ORM\Exception\UndefinedRelation  
Parameters
Parameter Type Description
$name string  

ORM\Entity::getRelation

↑ top


ORM\Entity::getTableName

public static function getTableName(): string
Get the table name

The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by $tableName.

Static   This method is static.
Returns string  

ORM\Entity::getTableName

↑ top


ORM\Entity::getTableNameTemplate

public static function getTableNameTemplate(): string

Warning: this method is deprecated. | Static | | This method is static. | | Returns | string | |

ORM\Entity::getTableNameTemplate

↑ top


ORM\Entity::hasLoaded

public function hasLoaded(string $relation): bool
Check if $relation got loaded already
Returns bool  
Parameters
Parameter Type Description
$relation string  

ORM\Entity::hasLoaded

↑ top


ORM\Entity::hasPrimaryKey

public function hasPrimaryKey(): bool
Check if the entity has has a complete primary key
Returns bool  

ORM\Entity::hasPrimaryKey

↑ top


ORM\Entity::isAutoIncremented

public static function isAutoIncremented(): bool
Check if the table has a auto increment column
Static   This method is static.
Returns bool  

ORM\Entity::isAutoIncremented

↑ top


ORM\Entity::isDirty

public function isDirty(string $attribute = null): bool
Checks if entity or $attribute got changed
Returns bool  
Parameters
Parameter Type Description
$attribute string Check only this variable or all variables

ORM\Entity::isDirty

↑ top


ORM\Entity::isValid

public function isValid(): bool|\ORM\Dbal\Error[]
Check if the current data is valid

Returns boolean true when valid otherwise an array of Errors.

Returns bool|ORM\Dbal\Error[]  

ORM\Entity::isValid

↑ top


ORM\Entity::isValidatorEnabled

public static function isValidatorEnabled(): bool
Check if the validator is enabled
Static   This method is static.
Returns bool  

ORM\Entity::isValidatorEnabled

↑ top


ORM\Entity::load

public function load(string $relation): $this

Nested relations can be loaded by separating them by “.” for example load all articles with comments from a user ($this): $user->load('articles.comments').

Returns $this  
Parameters
Parameter Type Description
$relation string  

ORM\Entity::load

↑ top


ORM\Entity::observeBy

public static function observeBy(
    \ORM\Observer\AbstractObserver|null $observer = null, 
): \ORM\Observer\CallbackObserver|null
Observe the class using $observer

If AbstractObserver is omitted it returns a new CallbackObserver. Usage example:

$em->observe(User::class)
    ->on('inserted', function (User $user) { ... })
    ->on('deleted', function (User $user) { ... });

For more information about model events please consult the [documentation](https://tflori.github.io/

Static   This method is static.
Returns ORM\Observer\CallbackObserver|null  
Parameters
Parameter Type Description  
$observer **ORM\Observer\AbstractObserver null**  

See Also:

ORM\Entity::observeBy

↑ top


ORM\Entity::onChange

public function onChange(
    string $attribute, 
    mixed $oldValue, 
    mixed $value, 
): mixed
Empty event handler

Get called when something is changed with magic setter.

Returns mixed  
Parameters
Parameter Type Description
$attribute string The variable that got changed.merge(node.inheritedProperties)
$oldValue mixed The old value of the variable
$value mixed The new value of the variable

ORM\Entity::onChange

↑ top


ORM\Entity::onInit

public function onInit(bool $new): mixed
Empty event handler

Get called when the entity get initialized.

Returns mixed  
Parameters
Parameter Type Description
$new bool Whether or not the entity is new or from database

ORM\Entity::onInit

↑ top


ORM\Entity::postPersist

public function postPersist(): mixed
Empty event handler

Get called after the entity got inserted in database.

Returns mixed  

ORM\Entity::postPersist

↑ top


ORM\Entity::postUpdate

public function postUpdate(): mixed
Empty event handler

Get called after the entity got updated in database.

Returns mixed  

ORM\Entity::postUpdate

↑ top


ORM\Entity::prePersist

public function prePersist(): mixed
Empty event handler

Get called before the entity get inserted in database.

Returns mixed  

ORM\Entity::prePersist

↑ top


ORM\Entity::preUpdate

public function preUpdate(): mixed
Empty event handler

Get called before the entity get updated in database.

Returns mixed  

ORM\Entity::preUpdate

↑ top


ORM\Entity::query

public static function query(): \ORM\EntityFetcher
Create an entityFetcher for this entity
Static   This method is static.
Returns ORM\EntityFetcher  

ORM\Entity::query

↑ top


ORM\Entity::registerGlobalFilter

public static function registerGlobalFilter(
    string|\ORM\EntityFetcher\FilterInterface|callable $filter, 
): mixed
Register a filter that is added to every fetcher for this entity
Static   This method is static.
Returns mixed  
Parameters
Parameter Type Description    
$filter **string ORM\EntityFetcher\FilterInterface callable**  

ORM\Entity::registerGlobalFilter

↑ top


ORM\Entity::reset

public function reset(string $attribute = null): mixed
Resets the entity or $attribute to original data
Returns mixed  
Parameters
Parameter Type Description
$attribute string Reset only this variable or all variables

ORM\Entity::reset

↑ top


ORM\Entity::resetRelated

public function resetRelated(null $relation = null): mixed
Resets all loaded relations or $relation

Helpful to reduce the size of serializations of the object (for caching, or toArray method etc.)

Returns mixed  
Parameters
Parameter Type Description
$relation null  

ORM\Entity::resetRelated

↑ top


ORM\Entity::save

public function save(): $this
Save the entity to EntityManager
Returns $this  
Throws ORM\Exception\IncompletePrimaryKey  

ORM\Entity::save

↑ top


ORM\Entity::serialize

public function serialize(): string
String representation of data
Returns string  

See Also:

ORM\Entity::serialize

↑ top


ORM\Entity::setAttribute

public function setAttribute(string $attribute, mixed $value): $this
Set $attribute to $value

Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store data that should be updated in the database to $data. Do not store data in $originalData as it will not be written and give wrong results for dirty checking.

The onChange event is called after something got changed.

The method throws an error when the validation fails (also when the column does not exist).

Returns $this  
Throws ORM\Dbal\Error  
Parameters
Parameter Type Description
$attribute string The variable to change
$value mixed The value to store

See Also:

ORM\Entity::setAttribute

↑ top


ORM\Entity::setEntityManager

public function setEntityManager(\ORM\EntityManager $entityManager): $this
Returns $this  
Parameters
Parameter Type Description
$entityManager ORM\EntityManager  

ORM\Entity::setEntityManager

↑ top


ORM\Entity::setNamingSchemeColumn

public static function setNamingSchemeColumn(string $namingSchemeColumn): mixed

Warning: this method is deprecated. | Static | | This method is static. | | Returns | mixed | |

Parameters
Parameter Type Description
$namingSchemeColumn string  

ORM\Entity::setNamingSchemeColumn

↑ top


ORM\Entity::setNamingSchemeMethods

public static function setNamingSchemeMethods(
    string $namingSchemeMethods, 
): mixed

Warning: this method is deprecated. | Static | | This method is static. | | Returns | mixed | |

Parameters
Parameter Type Description
$namingSchemeMethods string  

ORM\Entity::setNamingSchemeMethods

↑ top


ORM\Entity::setNamingSchemeTable

public static function setNamingSchemeTable(string $namingSchemeTable): mixed

Warning: this method is deprecated. | Static | | This method is static. | | Returns | mixed | |

Parameters
Parameter Type Description
$namingSchemeTable string  

ORM\Entity::setNamingSchemeTable

↑ top


ORM\Entity::setRelated

public function setRelated(string $relation, \ORM\Entity $entity = null): mixed
Set $relation to $entity

This method is only for the owner of a relation.

Returns mixed  
Parameters
Parameter Type Description
$relation string  
$entity ORM\Entity  

ORM\Entity::setRelated

↑ top


ORM\Entity::setTableNameTemplate

public static function setTableNameTemplate(string $tableNameTemplate): mixed

Warning: this method is deprecated. | Static | | This method is static. | | Returns | mixed | |

Parameters
Parameter Type Description
$tableNameTemplate string  

ORM\Entity::setTableNameTemplate

↑ top


ORM\Entity::toArray

public function toArray(
    array $attributes = [], 
    bool $includeRelations = true, 
    array $parents = [], 
): array
Get an array of the entity
Returns array  
Parameters
Parameter Type Description
$attributes array  
$includeRelations bool  
$parents array Prevent output containing toArray from parents

ORM\Entity::toArray

↑ top


ORM\Entity::unserialize

public function unserialize(string $serialized): mixed
Constructs the object
Returns mixed  
Parameters
Parameter Type Description
$serialized string The string representation of data

See Also:

ORM\Entity::unserialize

↑ top


ORM\Entity::validate

public static function validate(
    string $attribute, 
    mixed $value, 
): bool|\ORM\Dbal\Error
Validate $value for $attribute
Static   This method is static.
Returns bool|ORM\Dbal\Error  
Parameters
Parameter Type Description
$attribute string  
$value mixed  

ORM\Entity::validate

↑ top


ORM\Entity::validateArray

public static function validateArray(array $data): array
Validate $data

$data has to be an array of $attribute => $value

Static   This method is static.
Returns array  
Parameters
Parameter Type Description
$data array  

ORM\Entity::validateArray

↑ top