An entity represents a single result row from a repository. It exposes the methods for retrieving and storing properties associated in this row.
__construct( array $properties [] , array $options [] )
Initializes the internal properties of this entity out of the keys in an array. The following list of options can be used:
$entity = new Entity(['id' => 1, 'name' => 'Andrew'])
$properties
optional [] $options
optional [] __debugInfo( )
Returns an array that can be used to describe the internal state of this object.
array
__get( string $property )
Magic getter to access properties that have been set in this entity
$property
mixed
__isset( string $property )
Returns whether this entity contains a property named $property regardless of if it is empty.
$property
boolean
Cake\ORM\Entity::has()
__set( string $property , mixed $value )
Magic setter to add or edit a property in this entity
$property
$value
__toString( )
Returns a string representation of this object in a human readable format.
string
__unset( string $property )
Removes a property from this entity
$property
_methodExists( string $method )
Determines whether a method exists in this class
$method
boolean
_nestedErrors( string $field )
Auxiliary method for getting errors in nested entities
$field
array
_readError( array|Cake\Datasource\EntityTrait $object , string $path null )
Read the error(s) from one or many objects.
Cake\Datasource\EntityTrait
$object
$path
optional null array
accessible( string|array $property , boolean $set null )
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
$property
$set
optional null mixed
Cake\Datasource\EntityInterface::accessible()
clean( )
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
Cake\Datasource\EntityInterface::clean()
dirty( string $property null , null|boolean $isDirty null )
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
$property
optional null $isDirty
optional null boolean
Cake\Datasource\EntityInterface::dirty()
errors( string|array|null $field null , string|array|null $errors null , boolean $overwrite false )
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->errors('salary'); // Returns all error messages indexed by field name $entity->errors(); // Sets the error messages for multiple fields at once $entity->errors(['salary' => ['message'], 'name' => ['another message']);
When used as a setter, this method will return this entity instance for method chaining.
$field
optional null $errors
optional null $overwrite
optional false array|$this
Cake\Datasource\EntityInterface::errors()
extract( array $properties , boolean $onlyDirty false )
Returns an array with the requested properties stored in this entity, indexed by property name
$properties
$onlyDirty
optional false array
Cake\Datasource\EntityInterface::extract()
extractOriginal( array $properties )
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.
$properties
array
extractOriginalChanged( array $properties )
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.
$properties
array
get( string $property )
Returns the value of a property by name
$property
mixed
InvalidArgumentException
Cake\Datasource\EntityInterface::get()
getOriginal( string $property )
Returns the value of an original property by name
$property
mixed
InvalidArgumentException
has( string|array $property )
Returns whether this entity contains a property named $property regardless of if it is empty.
$entity = new Entity(['id' => 1, 'name' => null]); $entity->has('id'); // true $entity->has('name'); // false $entity->has('last_name'); // false
When checking multiple properties. All properties must not be null in order for true to be returned.
$property
boolean
Cake\Datasource\EntityInterface::has()
hiddenProperties( null|array $properties null )
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.
$properties
optional null array|$this
Cake\Datasource\EntityInterface::hiddenProperties()
isNew( boolean|null $new null )
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.
$new
optional null boolean
Cake\Datasource\EntityInterface::isNew()
jsonSerialize( )
Returns the properties that will be serialized as JSON
array
offsetExists( mixed $offset )
Implements isset($entity);
$offset
boolean
offsetGet( mixed $offset )
Implements $entity[$offset];
$offset
mixed
offsetSet( mixed $offset , mixed $value )
Implements $entity[$offset] = $value;
$offset
$value
offsetUnset( mixed $offset )
Implements unset($result[$offset);
$offset
set( string|array $property , mixed $value null , array $options [] )
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');
$property
$value
optional null $options
optional [] setter
and guard
mixed
InvalidArgumentException
Cake\Datasource\EntityInterface::set()
source( string $alias null )
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.
$alias
optional null string|$this
toArray( )
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.
array
Cake\Datasource\EntityInterface::toArray()
unsetProperty( string|array $property )
Removes a property or list of properties from this entity
$entity->unsetProperty('name'); $entity->unsetProperty(['name', 'last_name']);
$property
mixed
Cake\Datasource\EntityInterface::unsetProperty()
virtualProperties( null|array $properties null )
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.
$properties
optional null array|$this
Cake\Datasource\EntityInterface::virtualProperties()
visibleProperties( )
Get the list of visible properties.
The list of visible properties is all standard properties plus virtual properties minus hidden properties.
array
Cake\Datasource\EntityInterface::visibleProperties()
protected 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
['*' => true]
protected static array
Holds a cached list of methods that exist in the instanced class
[]
protected array
Holds a list of the properties that were modified or added after this object was originally created.
[]
protected array
List of property names that should not be included in JSON or Array representations of this Entity.
[]
protected 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.
true
protected array
Holds all properties that have been changed and their original values for this entity
[]
protected array
List of computed or virtual fields that should be included in JSON or array representations of this Entity. If a field is present in both _hidden and _virtual the field will not be in the array/json versions of the entity.
[]
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
http://api.cakephp.org/3.1/class-Cake.ORM.Entity.html