just another orm...
View the Project on GitHub tflori/orm
» API Reference » ORM\Entity
Implements: Serializable
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:
Name | Value |
---|---|
OPT_RELATION_CLASS | 'class' |
OPT_RELATION_CARDINALITY | 'cardinality' |
OPT_RELATION_REFERENCE | 'reference' |
OPT_RELATION_OPPONENT | 'opponent' |
OPT_RELATION_TABLE | 'table' |
final public function __construct(
array $data = [],
\ORM\EntityManager $entityManager = null,
bool $fromDatabase = false,
)
It calls ::onInit() after initializing $data and $originalData.
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
public function __get(string $attribute): mixed|null
Returns | mixed|null |
Parameter | Type | Description |
---|---|---|
$attribute |
string |
See Also:
ORM\Entity::__get
public function __isset(mixed $attribute): bool
Returns | bool |
Parameter | Type | Description |
---|---|---|
$attribute |
mixed |
ORM\Entity::__isset
public function __serialize(): mixed
Returns | mixed |
ORM\Entity::__serialize
public function __set(string $attribute, mixed $value): mixed
Returns | mixed |
Parameter | Type | Description |
---|---|---|
$attribute |
string | The variable to change |
$value |
mixed | The value to store |
See Also:
ORM\Entity::__set
public function __unserialize(mixed $data): mixed
Returns | mixed |
Parameter | Type | Description |
---|---|---|
$data |
mixed |
ORM\Entity::__unserialize
public function addRelated(string $relation, \ORM\Entity[] $entities): mixed
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 |
Parameter | Type | Description |
---|---|---|
$relation |
string | |
$entities |
ORM\Entity[] |
ORM\Entity::addRelated
public static function bootIfNotBooted(): mixed
Static | This method is static. | |
Returns | mixed |
ORM\Entity::bootIfNotBooted
public function delete(): $this
You might want to implement soft deletes by overriding this method.
Returns | $this |
ORM\Entity::delete
public function deleteRelated(string $relation, \ORM\Entity[] $entities): mixed
This method is only for many-to-many relations.
Returns | mixed |
Parameter | Type | Description |
---|---|---|
$relation |
string | |
$entities |
ORM\Entity[] |
ORM\Entity::deleteRelated
public static function describe(): \ORM\Dbal\Table|\ORM\Dbal\Column[]
Static | This method is static. | |
Returns | ORM\Dbal\Table|ORM\Dbal\Column[] |
ORM\Entity::describe
public static function detachObserver(
\ORM\Observer\AbstractObserver $observer,
): mixed
Static | This method is static. | |
Returns | mixed |
Parameter | Type | Description |
---|---|---|
$observer |
ORM\Observer\AbstractObserver |
See Also:
ORM\Entity::detachObserver
public static function disableValidator(bool $disable = true): mixed
Static | This method is static. | |
Returns | mixed |
Parameter | Type | Description |
---|---|---|
$disable |
bool |
ORM\Entity::disableValidator
public static function enableValidator(bool $enable = true): mixed
Static | This method is static. | |
Returns | mixed |
Parameter | Type | Description |
---|---|---|
$enable |
bool |
ORM\Entity::enableValidator
public function exists(): bool
Returns | bool |
ORM\Entity::exists
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 |
Parameter | Type | Description |
---|---|---|
$relation |
string | The relation to fetch |
$getAll |
bool |
ORM\Entity::fetch
public function fill(
array $data,
bool $ignoreUnknown = false,
bool $checkMissing = false,
): mixed
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 |
Parameter | Type | Description |
---|---|---|
$data |
array | |
$ignoreUnknown |
bool | |
$checkMissing |
bool |
ORM\Entity::fill
public function getAttribute(string $attribute): mixed|null
If there is a custom getter this method get called instead.
Returns | mixed|null |
Parameter | Type | Description |
---|---|---|
$attribute |
string | The variable to get |
See Also:
ORM\Entity::getAttribute
public static function getAttributeName(string $column): string
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 |
Parameter | Type | Description |
---|---|---|
$column |
string |
ORM\Entity::getAttributeName
public static function getColumnName(string $attribute): string
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 |
Parameter | Type | Description |
---|---|---|
$attribute |
string |
ORM\Entity::getColumnName
public function getDirty(): array
This method works on application level. Meaning it is showing additional attributes defined in ::$includedAttributes and and hiding ::$excludedAttributes.
Returns | array |
ORM\Entity::getDirty
public static function getNamingSchemeColumn(): string
Warning: this method is deprecated. | Static | | This method is static. | | Returns | string | |
ORM\Entity::getNamingSchemeColumn
public static function getNamingSchemeMethods(): string
Warning: this method is deprecated. | Static | | This method is static. | | Returns | string | |
ORM\Entity::getNamingSchemeMethods
public static function getNamingSchemeTable(): string
Warning: this method is deprecated. | Static | | This method is static. | | Returns | string | |
ORM\Entity::getNamingSchemeTable
public function getPrimaryKey(): array
Returns | array | |
Throws | ORM\Exception\IncompletePrimaryKey |
ORM\Entity::getPrimaryKey
public static function getPrimaryKeyVars(): array
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
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 |
Parameter | Type | Description |
---|---|---|
$relation |
string | |
$refresh |
bool |
ORM\Entity::getRelated
public static function getRelation(string $name): \ORM\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 |
Parameter | Type | Description |
---|---|---|
$name |
string |
ORM\Entity::getRelation
public static function getTableName(): string
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
public static function getTableNameTemplate(): string
Warning: this method is deprecated. | Static | | This method is static. | | Returns | string | |
ORM\Entity::getTableNameTemplate
public function hasLoaded(string $relation): bool
Returns | bool |
Parameter | Type | Description |
---|---|---|
$relation |
string |
ORM\Entity::hasLoaded
public function hasPrimaryKey(): bool
Returns | bool |
ORM\Entity::hasPrimaryKey
public static function isAutoIncremented(): bool
Static | This method is static. | |
Returns | bool |
ORM\Entity::isAutoIncremented
public function isDirty(string $attribute = null): bool
Returns | bool |
Parameter | Type | Description |
---|---|---|
$attribute |
string | Check only this variable or all variables |
ORM\Entity::isDirty
public function isValid(): bool|\ORM\Dbal\Error[]
Returns boolean true when valid otherwise an array of Errors.
Returns | bool|ORM\Dbal\Error[] |
ORM\Entity::isValid
public static function isValidatorEnabled(): bool
Static | This method is static. | |
Returns | bool |
ORM\Entity::isValidatorEnabled
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 |
Parameter | Type | Description |
---|---|---|
$relation |
string |
ORM\Entity::load
public static function observeBy(
\ORM\Observer\AbstractObserver|null $observer = null,
): \ORM\Observer\CallbackObserver|null
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 |
Parameter | Type | Description | |
---|---|---|---|
$observer |
**ORM\Observer\AbstractObserver | null** |
See Also:
ORM\Entity::observeBy
public function onChange(
string $attribute,
mixed $oldValue,
mixed $value,
): mixed
Get called when something is changed with magic setter.
Returns | mixed |
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
public function onInit(bool $new): mixed
Get called when the entity get initialized.
Returns | mixed |
Parameter | Type | Description |
---|---|---|
$new |
bool | Whether or not the entity is new or from database |
ORM\Entity::onInit
public function postPersist(): mixed
Get called after the entity got inserted in database.
Returns | mixed |
ORM\Entity::postPersist
public function postUpdate(): mixed
Get called after the entity got updated in database.
Returns | mixed |
ORM\Entity::postUpdate
public function prePersist(): mixed
Get called before the entity get inserted in database.
Returns | mixed |
ORM\Entity::prePersist
public function preUpdate(): mixed
Get called before the entity get updated in database.
Returns | mixed |
ORM\Entity::preUpdate
public static function query(): \ORM\EntityFetcher
Static | This method is static. | |
Returns | ORM\EntityFetcher |
ORM\Entity::query
public static function registerGlobalFilter(
string|\ORM\EntityFetcher\FilterInterface|callable $filter,
): mixed
Static | This method is static. | |
Returns | mixed |
Parameter | Type | Description | ||
---|---|---|---|---|
$filter |
**string | ORM\EntityFetcher\FilterInterface | callable** |
ORM\Entity::registerGlobalFilter
public function reset(string $attribute = null): mixed
Returns | mixed |
Parameter | Type | Description |
---|---|---|
$attribute |
string | Reset only this variable or all variables |
ORM\Entity::reset
public function resetRelated(null $relation = null): mixed
Helpful to reduce the size of serializations of the object (for caching, or toArray method etc.)
Returns | mixed |
Parameter | Type | Description |
---|---|---|
$relation |
null |
ORM\Entity::resetRelated
public function save(): $this
Returns | $this | |
Throws | ORM\Exception\IncompletePrimaryKey |
ORM\Entity::save
public function serialize(): string
Returns | string |
See Also:
ORM\Entity::serialize
public function setAttribute(string $attribute, mixed $value): $this
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 |
Parameter | Type | Description |
---|---|---|
$attribute |
string | The variable to change |
$value |
mixed | The value to store |
See Also:
ORM\Entity::setAttribute
public function setEntityManager(\ORM\EntityManager $entityManager): $this
Returns | $this |
Parameter | Type | Description |
---|---|---|
$entityManager |
ORM\EntityManager |
ORM\Entity::setEntityManager
public static function setNamingSchemeColumn(string $namingSchemeColumn): mixed
Warning: this method is deprecated. | Static | | This method is static. | | Returns | mixed | |
Parameter | Type | Description |
---|---|---|
$namingSchemeColumn |
string |
ORM\Entity::setNamingSchemeColumn
public static function setNamingSchemeMethods(
string $namingSchemeMethods,
): mixed
Warning: this method is deprecated. | Static | | This method is static. | | Returns | mixed | |
Parameter | Type | Description |
---|---|---|
$namingSchemeMethods |
string |
ORM\Entity::setNamingSchemeMethods
public static function setNamingSchemeTable(string $namingSchemeTable): mixed
Warning: this method is deprecated. | Static | | This method is static. | | Returns | mixed | |
Parameter | Type | Description |
---|---|---|
$namingSchemeTable |
string |
ORM\Entity::setNamingSchemeTable
public function setRelated(string $relation, \ORM\Entity $entity = null): mixed
This method is only for the owner of a relation.
Returns | mixed |
Parameter | Type | Description |
---|---|---|
$relation |
string | |
$entity |
ORM\Entity |
ORM\Entity::setRelated
public static function setTableNameTemplate(string $tableNameTemplate): mixed
Warning: this method is deprecated. | Static | | This method is static. | | Returns | mixed | |
Parameter | Type | Description |
---|---|---|
$tableNameTemplate |
string |
ORM\Entity::setTableNameTemplate
public function toArray(
array $attributes = [],
bool $includeRelations = true,
array $parents = [],
): array
Returns | array |
Parameter | Type | Description |
---|---|---|
$attributes |
array | |
$includeRelations |
bool | |
$parents |
array | Prevent output containing toArray from parents |
ORM\Entity::toArray
public function unserialize(string $serialized): mixed
Returns | mixed |
Parameter | Type | Description |
---|---|---|
$serialized |
string | The string representation of data |
See Also:
ORM\Entity::unserialize
public static function validate(
string $attribute,
mixed $value,
): bool|\ORM\Dbal\Error
Static | This method is static. | |
Returns | bool|ORM\Dbal\Error |
Parameter | Type | Description |
---|---|---|
$attribute |
string | |
$value |
mixed |
ORM\Entity::validate
public static function validateArray(array $data): array
$data has to be an array of $attribute => $value
Static | This method is static. | |
Returns | array |
Parameter | Type | Description |
---|---|---|
$data |
array |
ORM\Entity::validateArray