CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • Association
  • AssociationCollection
  • Behavior
  • BehaviorRegistry
  • EagerLoader
  • Entity
  • Marshaller
  • Query
  • ResultSet
  • RulesChecker
  • SaveOptionsBuilder
  • Table
  • TableRegistry

Interfaces

  • PropertyMarshalInterface

Traits

  • AssociationsNormalizerTrait

Class Entity

An entity represents a single result row from a repository. It exposes the methods for retrieving and storing properties associated in this row.

Cake\ORM\Entity implements Cake\Datasource\EntityInterface, Cake\Datasource\InvalidPropertyInterface uses Cake\Datasource\EntityTrait
Namespace: Cake\ORM
Location: ORM/Entity.php

Inherited Properties

  • _accessible, _accessors, _className, _dirty, _errors, _hidden, _invalid, _new, _original, _properties, _registryAlias, _virtual

Method Summary

  • __construct() public

    Initializes the internal properties of this entity out of the keys in an array. The following list of options can be used:

Method Detail

__construct() public ¶

__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:

  • useSetters: whether use internal setters for properties or not
  • markClean: whether to mark all properties as clean after setting them
  • markNew: whether this instance has not yet been persisted
  • guard: whether to prevent inaccessible properties from being set (default: false)
  • source: A string representing the alias of the repository this entity came from

Example:

$entity = new Entity(['id' => 1, 'name' => 'Andrew'])
Parameters
array $properties optional []
hash of properties to set in this entity
array $options optional []
list of options to use when creating this entity

Methods used from Cake\Datasource\EntityTrait

__debugInfo() public ¶

__debugInfo( )

Returns an array that can be used to describe the internal state of this object.

Returns
array

__get() public ¶

__get( string $property )

Magic getter to access properties that have been set in this entity

Parameters
string $property
Name of the property to access
Returns
mixed

__isset() public ¶

__isset( string $property )

Returns whether this entity contains a property named $property regardless of if it is empty.

Parameters
string $property
The property to check.
Returns
boolean
See
\Cake\ORM\Entity::has()

__set() public ¶

__set( string $property , mixed $value )

Magic setter to add or edit a property in this entity

Parameters
string $property
The name of the property to set
mixed $value
The value to set to the property

__toString() public ¶

__toString( )

Returns a string representation of this object in a human readable format.

Returns
string

__unset() public ¶

__unset( string $property )

Removes a property from this entity

Parameters
string $property
The property to unset

_accessor() protected static ¶

_accessor( string $property , string $type )

Fetch accessor method name Accessor methods (available or not) are cached in $_accessors

Parameters
string $property
the field name to derive getter name from
string $type
the accessor type ('get' or 'set')
Returns
string
method name or empty string (no method available)

_nestedErrors() protected ¶

_nestedErrors( string $field )

Auxiliary method for getting errors in nested entities

Parameters
string $field
the field in this entity to check for errors
Returns
array
errors in nested entity if any

_readError() protected ¶

_readError( array|Cake\Datasource\EntityInterface $object , string|null $path = null )

Read the error(s) from one or many objects.

Parameters
array|Cake\Datasource\EntityInterface $object
The object to read errors from.
string|null $path optional null
The field name for errors.
Returns
array

_readHasErrors() protected ¶

_readHasErrors( mixed $object )

Reads if there are errors for one or many objects.

Parameters
mixed $object
The object to read errors from.
Returns
boolean

accessible() public deprecated ¶

accessible( string|array $property , boolean|null $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.

Example:

$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.

Example:

$entity->accessible('id'); // Returns whether it can be set or not
Deprecated
3.4.0 Use EntityTrait::setAccess() and EntityTrait::isAccessible()
Parameters
string|array $property
single or list of properties to change its accessibility
boolean|null $set optional null

true marks the property as accessible, false will mark it as protected.

Returns

$this|bool

clean() public ¶

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

dirty() public deprecated ¶

dirty( string|null $property = null , boolean|null $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

Deprecated
3.4.0 Use EntityTrait::setDirty() and EntityTrait::isDirty()
Parameters
string|null $property optional null
the field to set or check status for
boolean|null $isDirty optional null

true means the property was changed, false means it was not changed and null will make the function return current state for that property

Returns
boolean
Whether the property was changed or not

errors() public deprecated ¶

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.

Example

// 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.

Deprecated
3.4.0 Use EntityTrait::setError(), EntityTrait::setErrors(), EntityTrait::getError() and EntityTrait::getErrors()
Parameters
string|array|null $field optional null
The field to get errors for, or the array of errors to set.
string|array|null $errors optional null
The errors to be set for $field
boolean $overwrite optional false
Whether or not to overwrite pre-existing errors for $field
Returns
array|Cake\Datasource\EntityTrait
$this

extract() public ¶

extract( array $properties , boolean $onlyDirty = false )

Returns an array with the requested properties stored in this entity, indexed by property name

Parameters
array $properties
list of properties to be returned
boolean $onlyDirty optional false
Return the requested property only if it is dirty
Returns
array

extractOriginal() public ¶

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.

Parameters
array $properties
List of properties to be returned
Returns
array

extractOriginalChanged() public ¶

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.

Parameters
array $properties
List of properties to be returned
Returns
array

get() public ¶

get( string $property )

Returns the value of a property by name

Parameters
string $property
the name of the property to retrieve
Returns
mixed
Throws
InvalidArgumentException
if an empty property name is passed

getDirty() public ¶

getDirty( )

Gets the dirty properties.

Returns
string[]

getError() public ¶

getError( string $field )

Returns validation errors of a field

Parameters
string $field
Field name to get the errors from
Returns
array

getErrors() public ¶

getErrors( )

Returns all validation errors.

Returns
array

getHidden() public ¶

getHidden( )

Gets the hidden properties.

Returns
array

getInvalid() public ¶

getInvalid( )

Get a list of invalid fields and their data for errors upon validation/patching

Returns
array

getInvalidField() public ¶

getInvalidField( string $field )

Get a single value of an invalid field. Returns null if not set.

Parameters
string $field
The name of the field.
Returns
mixed|null

getOriginal() public ¶

getOriginal( string $property )

Returns the value of an original property by name

Parameters
string $property
the name of the property for which original value is retrieved.
Returns
mixed
Throws
InvalidArgumentException
if an empty property name is passed.

getOriginalValues() public ¶

getOriginalValues( )

Gets all original values of the entity.

Returns
array

getSource() public ¶

getSource( )

Returns the alias of the repository from which this entity came from.

Returns
string

getVirtual() public ¶

getVirtual( )

Gets the virtual properties on this entity.

Returns
array

getVisible() public ¶

getVisible( )

Gets the list of visible properties.

The list of visible properties is all standard properties plus virtual properties minus hidden properties.

Returns
array

A list of properties that are 'visible' in all representations.


has() public ¶

has( string|array $property )

Returns whether this entity contains a property named $property that contains a non-null value.

Example:

$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.

Parameters
string|array $property
The property or properties to check.
Returns
boolean

hasErrors() public ¶

hasErrors( boolean $includeNested = true )

Returns whether this entity has errors.

Parameters
boolean $includeNested optional true
true will check nested entities for hasErrors()
Returns
boolean

hasValue() public ¶

hasValue( string $property )

Checks tha a property has a value.

This method will return true for

  • Non-empty strings
  • Non-empty arrays
  • Any object
  • Integer, even 0
  • Float, even 0.0

and false in all other cases.

Parameters
string $property
The property to check.
Returns
boolean

hiddenProperties() public deprecated ¶

hiddenProperties( array|null $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.

Deprecated
3.4.0 Use EntityTrait::setHidden() and EntityTrait::getHidden()
Parameters
array|null $properties optional null
Either an array of properties to hide or null to get properties
Returns
array|Cake\Datasource\EntityTrait
$this

invalid() public deprecated ¶

invalid( string|array|null $field = null , mixed|null $value = null , boolean $overwrite = false )

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.

Deprecated
3.5 Use getInvalid()/getInvalidField()/setInvalid() instead.
Parameters
string|array|null $field optional null
The field to get invalid value for, or the value to set.
mixed|null $value optional null
The invalid value to be set for $field.
boolean $overwrite optional false
Whether or not to overwrite pre-existing values for $field.
Returns

$this|mixed

isAccessible() public ¶

isAccessible( string $property )

Checks if a property is accessible

Example:

$entity->isAccessible('id'); // Returns whether it can be set or not
Parameters
string $property
Property name to check
Returns
boolean

isDirty() public ¶

isDirty( string|null $property = null )

Checks if the entity is dirty or if a single property of it is dirty.

Parameters
string|null $property optional null
The field to check the status for. Null for the whole entity.
Returns
boolean
Whether the property was changed or not

isEmpty() public ¶

isEmpty( string $property )

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.

Parameters
string $property
The property to check.
Returns
boolean

isNew() public ¶

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.

Parameters
boolean|null $new optional null
true if it is known this instance was not yet persisted
Returns
boolean
Whether or not the entity has been persisted.

jsonSerialize() public ¶

jsonSerialize( )

Returns the properties that will be serialized as JSON

Returns
array

offsetExists() public ¶

offsetExists( mixed $offset )

Implements isset($entity);

Parameters
mixed $offset
The offset to check.
Returns
boolean
Success

offsetGet() public ¶

offsetGet( mixed $offset )

Implements $entity[$offset];

Parameters
mixed $offset
The offset to get.
Returns
mixed

offsetSet() public ¶

offsetSet( mixed $offset , mixed $value )

Implements $entity[$offset] = $value;

Parameters
mixed $offset
The offset to set.
mixed $value
The value to set.

offsetUnset() public ¶

offsetUnset( mixed $offset )

Implements unset($result[$offset]);

Parameters
mixed $offset
The offset to remove.

set() public ¶

set( string|array $property , mixed $value = null , array $options = [] )

Sets a single property inside this entity.

Example:

$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

Example:

$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');
Parameters
string|array $property

the name of property to set or a list of properties with their respective values

mixed $value optional null

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 optional []

options to be used for setting the property. Allowed option keys are setter and guard

Returns

$this
Throws
InvalidArgumentException

setAccess() public ¶

setAccess( string|array $property , boolean $set )

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.

Example:

$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
Parameters
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.

Returns

$this

setDirty() public ¶

setDirty( string $property , boolean $isDirty = true )

Sets the dirty status of a single property.

Parameters
string $property
the field to set or check status for
boolean $isDirty optional true

true means the property was changed, false means it was not changed. Defaults to true.

Returns

$this

setError() public ¶

setError( string $field , string|array $errors , boolean $overwrite = false )

Sets errors for a single field

Example

// Sets the error messages for a single field
$entity->setError('salary', ['must be numeric', 'must be a positive number']);
Parameters
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 optional false
Whether or not to overwrite pre-existing errors for $field
Returns

$this

setErrors() public ¶

setErrors( array $errors , boolean $overwrite = false )

Sets error messages to the entity

Example

// Sets the error messages for multiple fields at once
$entity->setErrors(['salary' => ['message'], 'name' => ['another message']]);
Parameters
array $errors
The array of errors to set.
boolean $overwrite optional false
Whether or not to overwrite pre-existing errors for $fields
Returns

$this

setHidden() public ¶

setHidden( array $properties , boolean $merge = false )

Sets hidden properties.

Parameters
array $properties
An array of properties to hide from array exports.
boolean $merge optional false
Merge the new properties with the existing. By default false.
Returns

$this

setInvalid() public ¶

setInvalid( array $fields , boolean $overwrite = false )

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.

Parameters
array $fields
The values to set.
boolean $overwrite optional false
Whether or not to overwrite pre-existing values for $field.
Returns

$this

setInvalidField() public ¶

setInvalidField( string $field , mixed $value )

Sets a field as invalid and not patchable into the entity.

Parameters
string $field
The value to set.
mixed $value
The invalid value to be set for $field.
Returns

$this

setSource() public ¶

setSource( string $alias )

Sets the source alias

Parameters
string $alias
the alias of the repository
Returns

$this

setVirtual() public ¶

setVirtual( array $properties , boolean $merge = false )

Sets the virtual properties on this entity.

Parameters
array $properties
An array of properties to treat as virtual.
boolean $merge optional false
Merge the new properties with the existing. By default false.
Returns

$this

source() public deprecated ¶

source( string|null $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.

Deprecated
3.4.0 Use EntityTrait::getSource() and EntityTrait::setSource()
Parameters
string|null $alias optional null
the alias of the repository
Returns
string|Cake\Datasource\EntityTrait
$this

toArray() public ¶

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.

Returns
array

unsetProperty() public ¶

unsetProperty( string|array $property )

Removes a property or list of properties from this entity

Examples:

$entity->unsetProperty('name');
$entity->unsetProperty(['name', 'last_name']);
Parameters
string|array $property
The property to unset.
Returns

$this

virtualProperties() public deprecated ¶

virtualProperties( array|null $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.

Deprecated
3.4.0 Use EntityTrait::getVirtual() and EntityTrait::setVirtual()
Parameters
array|null $properties optional null
Either an array of properties to treat as virtual or null to get properties
Returns
array|Cake\Datasource\EntityTrait
$this

visibleProperties() public deprecated ¶

visibleProperties( )

Gets the list of visible properties.

The list of visible properties is all standard properties plus virtual properties minus hidden properties.

Deprecated
3.8.0 Use getVisible() instead.
Returns
array

A list of properties that are 'visible' in all representations.


Magic methods inherited from Cake\Datasource\EntityInterface

extractOriginal(), extractOriginalChanged(), getError(), getErrors(), getHidden(), getSource(), getVirtual(), getVisible(), hasErrors(), isAccessible(), isDirty(), setError(), setErrors()

Magic methods inherited from Cake\Datasource\InvalidPropertyInterface

getInvalid(), getInvalidField()
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs