$_properties
$_properties : array
Holds all properties and their values for this entity
An entity represents a single result row from a repository. It exposes the methods for retrieving and storing properties associated in this row.
$_hidden : array
List of property names that should **not** be included in JSON or Array representations of this Entity.
$_dirty : array
Holds a list of the properties that were modified or added after this object was originally created.
None found |
None found |
$_new : boolean
Indicates whether or not this entity is yet to be persisted.
Entities default to assuming they are new. You can use Table::persisted() to set the new flag on an entity based on records in the database.
None found |
None found |
$_invalid : array
List of invalid fields and their data for errors upon validation/patching
None found |
$_accessible : array
Map of properties in this entity that can be safely assigned, each property name points to a boolean indicating its status. An empty array means no properties are accessible
The special property '*' can also be mapped, meaning that any other property
not defined in the map will take its value. For example, '\*' => true
means that any property not defined in the map will be accessible by default
None found |
$_registryAlias : string
The alias of the repository this entity came from
None found |
__get(string $property) : mixed
Magic getter to access properties that have been set in this entity
string | $property | Name of the property to access |
None found |
__set(string $property, mixed $value) : void
Magic setter to add or edit a property in this entity
string | $property | The name of the property to set |
mixed | $value | The value to set to the property |
None found |
__isset(string $property) : boolean
Returns whether this entity contains a property named $property regardless of if it is empty.
string | $property | The property to check. |
None found |
__unset(string $property) : void
Removes a property from this entity
string | $property | The property to unset |
None found |
set(string|array $property, mixed $value = null, array $options = array()) : $this
Sets a single property inside this entity.
$entity->set('name', 'Andrew');
It is also possible to mass-assign multiple properties to this entity with one call by passing a hashed array as properties in the form of property => value pairs
$entity->set(['name' => 'andrew', 'id' => 1]);
echo $entity->name // prints andrew
echo $entity->id // prints 1
Some times it is handy to bypass setter functions in this entity when assigning
properties. You can achieve this by disabling the setter
option using the
$options
parameter:
$entity->set('name', 'Andrew', ['setter' => false]);
$entity->set(['name' => 'Andrew', 'id' => 1], ['setter' => false]);
Mass assignment should be treated carefully when accepting user input, by default
entities will guard all fields when properties are assigned in bulk. You can disable
the guarding for a single set call with the guard
option:
$entity->set(['name' => 'Andrew', 'id' => 1], ['guard' => true]);
You do not need to use the guard option when assigning properties individually:
// No need to use the guard option.
$entity->set('name', 'Andrew');
string|array | $property | the name of property to set or a list of properties with their respective values |
mixed | $value | The value to set to the property or an array if the first argument is also an array, in which case will be treated as $options |
array | $options | options to be used for setting the property. Allowed option
keys are |
None found |
get(string $property) : mixed
Returns the value of a property by name
string | $property | the name of the property to retrieve |
if an empty property name is passed
None found |
getOriginal(string $property) : mixed
Returns the value of an original property by name
string | $property | the name of the property for which original value is retrieved. |
if an empty property name is passed.
None found |
getOriginalValues() : array
Gets all original values of the entity.
None found |
has(string|array $property) : boolean
Returns whether this entity contains a property named $property that contains a non-null value.
$entity = new Entity(['id' => 1, 'name' => null]);
$entity->has('id'); // true
$entity->has('name'); // false
$entity->has('last_name'); // false
You can check multiple properties by passing an array:
$entity->has(['name', 'last_name']);
All properties must not be null to get a truthy result.
When checking multiple properties. All properties must not be null in order for true to be returned.
string|array | $property | The property or properties to check. |
None found |
isEmpty(string $property) : boolean
Checks that a property is empty
This is not working like the PHP empty()
function. The method will
return true for:
''
(empty string)null
[]
and false in all other cases.
string | $property | The property to check. |
None found |
hasValue(string $property) : boolean
Checks tha a property has a value.
This method will return true for
0
and false in all other cases.
string | $property | The property to check. |
None found |
unsetProperty(string|array $property) : $this
Removes a property or list of properties from this entity
$entity->unsetProperty('name');
$entity->unsetProperty(['name', 'last_name']);
string|array | $property | The property to unset. |
None found |
hiddenProperties(null|array $properties = null) : array|$this
Get/Set the hidden properties on this entity.
If the properties argument is null, the currently hidden properties will be returned. Otherwise the hidden properties will be set.
null|array | $properties | Either an array of properties to hide or null to get properties |
None found |
setHidden(array $properties, boolean $merge = false) : $this
Sets hidden properties.
array | $properties | An array of properties to hide from array exports. |
boolean | $merge | Merge the new properties with the existing. By default false. |
None found |
None found |
virtualProperties(null|array $properties = null) : array|$this
Get/Set the virtual properties on this entity.
If the properties argument is null, the currently virtual properties will be returned. Otherwise the virtual properties will be set.
null|array | $properties | Either an array of properties to treat as virtual or null to get properties |
None found |
setVirtual(array $properties, boolean $merge = false) : $this
Sets the virtual properties on this entity.
array | $properties | An array of properties to treat as virtual. |
boolean | $merge | Merge the new properties with the existing. By default false. |
None found |
None found |
visibleProperties() : array
Get the list of visible properties.
The list of visible properties is all standard properties plus virtual properties minus hidden properties.
A list of properties that are 'visible' in all representations.
None found |
toArray() : array
Returns an array with all the properties that have been set to this entity
This method will recursively transform entities assigned to properties into arrays as well.
None found |
jsonSerialize() : array
Returns the properties that will be serialized as JSON
None found |
offsetExists(mixed $offset) : boolean
Implements isset($entity);
mixed | $offset | The offset to check. |
Success
None found |
offsetGet(mixed $offset) : mixed
Implements $entity[$offset];
mixed | $offset | The offset to get. |
None found |
offsetSet(mixed $offset, mixed $value) : void
Implements $entity[$offset] = $value;
mixed | $offset | The offset to set. |
mixed | $value | The value to set. |
None found |
offsetUnset(mixed $offset) : void
Implements unset($result[$offset]);
mixed | $offset | The offset to remove. |
None found |
extract(array $properties, boolean $onlyDirty = false) : array
Returns an array with the requested properties stored in this entity, indexed by property name
array | $properties | list of properties to be returned |
boolean | $onlyDirty | Return the requested property only if it is dirty |
None found |
extractOriginal(array $properties) : array
Returns an array with the requested original properties stored in this entity, indexed by property name.
Properties that are unchanged from their original value will be included in the return of this method.
array | $properties | List of properties to be returned |
None found |
extractOriginalChanged(array $properties) : array
Returns an array with only the original properties stored in this entity, indexed by property name.
This method will only return properties that have been modified since the entity was built. Unchanged properties will be omitted.
array | $properties | List of properties to be returned |
None found |
dirty(string|null $property = null, null|boolean $isDirty = null) : boolean
Sets the dirty status of a single property. If called with no second argument, it will return whether the property was modified or not after the object creation.
When called with no arguments it will return whether or not there are any dirty property in the entity
string|null | $property | the field to set or check status for |
null|boolean | $isDirty | true means the property was changed, false means it was not changed and null will make the function return current state for that property |
Whether the property was changed or not
None found |
setDirty(string $property, boolean $isDirty = true) : $this
Sets the dirty status of a single property.
string | $property | the field to set or check status for |
boolean | $isDirty | true means the property was changed, false means it was not changed. Defaults to true. |
None found |
isDirty(string|null $property = null) : boolean
Checks if the entity is dirty or if a single property of it is dirty.
string|null | $property | The field to check the status for. Null for the whole entity. |
Whether the property was changed or not
None found |
None found |
clean() : void
Sets the entire entity as clean, which means that it will appear as no properties being modified or added at all. This is an useful call for an initial object hydration
None found |
isNew(boolean|null $new = null) : boolean
Returns whether or not this entity has already been persisted.
This method can return null in the case there is no prior information on the status of this entity.
If called with a boolean it will set the known status of this instance, true means that the instance is not yet persisted in the database, false that it already is.
boolean|null | $new | true if it is known this instance was not yet persisted |
Whether or not the entity has been persisted.
None found |
hasErrors(boolean $includeNested = true) : boolean
Returns whether this entity has errors.
boolean | $includeNested | true will check nested entities for hasErrors() |
None found |
None found |
getError(string $field) : array
Returns validation errors of a field
string | $field | Field name to get the errors from |
None found |
setErrors(array $fields, boolean $overwrite = false) : $this
Sets error messages to the entity
// Sets the error messages for multiple fields at once
$entity->setErrors(['salary' => ['message'], 'name' => ['another message']]);
array | $fields | The array of errors to set. |
boolean | $overwrite | Whether or not to overwrite pre-existing errors for $fields |
None found |
setError(string $field, string|array $errors, boolean $overwrite = false) : $this
Sets errors for a single field
// Sets the error messages for a single field
$entity->setError('salary', ['must be numeric', 'must be a positive number']);
string | $field | The field to get errors for, or the array of errors to set. |
string|array | $errors | The errors to be set for $field |
boolean | $overwrite | Whether or not to overwrite pre-existing errors for $field |
None found |
errors(string|array|null $field = null, string|array|null $errors = null, boolean $overwrite = false) : array|$this
Sets the error messages for a field or a list of fields. When called without the second argument it returns the validation errors for the specified fields. If called with no arguments it returns all the validation error messages stored in this entity and any other nested entity.
// Sets the error messages for a single field
$entity->errors('salary', ['must be numeric', 'must be a positive number']);
// Returns the error messages for a single field
$entity->getErrors('salary');
// Returns all error messages indexed by field name
$entity->getErrors();
// Sets the error messages for multiple fields at once
$entity->getErrors(['salary' => ['message'], 'name' => ['another message']);
When used as a setter, this method will return this entity instance for method chaining.
string|array|null | $field | The field to get errors for, or the array of errors to set. |
string|array|null | $errors | The errors to be set for $field |
boolean | $overwrite | Whether or not to overwrite pre-existing errors for $field |
None found |
getInvalid() : array
Get a list of invalid fields and their data for errors upon validation/patching
None found |
getInvalidField(string $field) : mixed
Get a single value of an invalid field. Returns null if not set.
string | $field | The name of the field. |
None found |
setInvalid(array $fields, boolean $overwrite = false) : $this
Set fields as invalid and not patchable into the entity.
This is useful for batch operations when one needs to get the original value for an error message after patching. This value could not be patched into the entity and is simply copied into the _invalid property for debugging purposes or to be able to log it away.
array | $fields | The values to set. |
boolean | $overwrite | Whether or not to overwrite pre-existing values for $field. |
None found |
setInvalidField(string $field, mixed $value) : $this
Sets a field as invalid and not patchable into the entity.
string | $field | The value to set. |
mixed | $value | The invalid value to be set for $field. |
None found |
invalid(string|array|null $field = null, mixed|null $value = null, boolean $overwrite = false) : $this|mixed
Sets a field as invalid and not patchable into the entity.
This is useful for batch operations when one needs to get the original value for an error message after patching. This value could not be patched into the entity and is simply copied into the _invalid property for debugging purposes or to be able to log it away.
string|array|null | $field | The field to get invalid value for, or the value to set. |
mixed|null | $value | The invalid value to be set for $field. |
boolean | $overwrite | Whether or not to overwrite pre-existing values for $field. |
None found |
accessible(string|array $property, boolean|null $set = null) : $this|boolean
Stores whether or not a property value can be changed or set in this entity.
The special property *
can also be marked as accessible or protected, meaning
that any other property specified before will take its value. For example
$entity->accessible('*', true)
means that any property not specified already
will be accessible by default.
You can also call this method with an array of properties, in which case they will each take the accessibility value specified in the second argument.
$entity->accessible('id', true); // Mark id as not protected
$entity->accessible('author_id', false); // Mark author_id as protected
$entity->accessible(['id', 'user_id'], true); // Mark both properties as accessible
$entity->accessible('*', false); // Mark all properties as protected
When called without the second param it will return whether or not the property can be set.
$entity->accessible('id'); // Returns whether it can be set or not
string|array | $property | single or list of properties to change its accessibility |
boolean|null | $set | true marks the property as accessible, false will mark it as protected. |
None found |
setAccess(string|array $property, boolean $set) : $this
Stores whether or not a property value can be changed or set in this entity.
The special property *
can also be marked as accessible or protected, meaning
that any other property specified before will take its value. For example
$entity->setAccess('*', true)
means that any property not specified already
will be accessible by default.
You can also call this method with an array of properties, in which case they will each take the accessibility value specified in the second argument.
$entity->setAccess('id', true); // Mark id as not protected
$entity->setAccess('author_id', false); // Mark author_id as protected
$entity->setAccess(['id', 'user_id'], true); // Mark both properties as accessible
$entity->setAccess('*', false); // Mark all properties as protected
string|array | $property | single or list of properties to change its accessibility |
boolean | $set | true marks the property as accessible, false will mark it as protected. |
None found |
isAccessible(string $property) : boolean
Checks if a property is accessible
$entity->isAccessible('id'); // Returns whether it can be set or not
string | $property | Property name to check |
None found |
getSource() : string
Returns the alias of the repository from which this entity came from.
None found |
setSource(string $alias) : $this
Sets the source alias
string | $alias | the alias of the repository |
None found |
source(string|null $alias = null) : string|$this
Returns the alias of the repository from which this entity came from.
If called with no arguments, it returns the alias of the repository this entity came from if it is known.
string|null | $alias | the alias of the repository |
None found |
__toString() : string
Returns a string representation of this object in a human readable format.
None found |
__debugInfo() : array
Returns an array that can be used to describe the internal state of this object.
None found |
_accessor(string $property, string $type) : string
Fetch accessor method name Accessor methods (available or not) are cached in $_accessors
string | $property | the field name to derive getter name from |
string | $type | the accessor type ('get' or 'set') |
method name or empty string (no method available)
None found |
_nestedErrors(string $field) : array
Auxiliary method for getting errors in nested entities
string | $field | the field in this entity to check for errors |
errors in nested entity if any
None found |
_readHasErrors(mixed $object) : boolean
Reads if there are errors for one or many objects.
mixed | $object | The object to read errors from. |
None found |
_readError(array|\Cake\Datasource\EntityInterface $object, string|null $path = null) : array
Read the error(s) from one or many objects.
array|\Cake\Datasource\EntityInterface | $object | The object to read errors from. |
string|null | $path | The field name for errors. |
None found |