Fr3nch13/CakePHP Utilities

Book extends Entity
in package

FinalYes

Book Entity

Table of Contents

Properties

$id  : int
$name  : string
$student  : Student
$student_id  : int
$_accessible  : array<string, bool>
Map of fields in this entity that can be safely assigned, each field name points to a boolean indicating its status. An empty array means no fields are accessible
$_accessors  : array<string, array<string, array<string, string>>>
Holds a cached list of getters/setters per class
$_dirty  : array<string|int, bool>
Holds a list of the fields that were modified or added after this object was originally created.
$_errors  : array<string, mixed>
List of errors per field as stored in this object.
$_fields  : array<string, mixed>
Holds all fields and their values for this entity.
$_hidden  : array<string|int, string>
List of field names that should **not** be included in JSON or Array representations of this Entity.
$_invalid  : array<string, mixed>
List of invalid fields and their data for errors upon validation/patching.
$_new  : bool
Indicates whether this entity is yet to be persisted.
$_original  : array<string, mixed>
Holds all fields that have been changed and their original values for this entity.
$_registryAlias  : string
The alias of the repository this entity came from
$_virtual  : array<string|int, string>
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.

Methods

__construct()  : mixed
Initializes the internal properties of this entity out of the keys in an array. The following list of options can be used:
__debugInfo()  : array<string, mixed>
Returns an array that can be used to describe the internal state of this object.
__get()  : mixed
Magic getter to access fields that have been set in this entity
__isset()  : bool
Returns whether this entity contains a field named $field and is not set to null.
__set()  : void
Magic setter to add or edit a field in this entity
__toString()  : string
Returns a string representation of this object in a human readable format.
__unset()  : void
Removes a field from this entity
clean()  : void
Sets the entire entity as clean, which means that it will appear as no fields being modified or added at all. This is an useful call for an initial object hydration
extract()  : array<string|int, mixed>
Returns an array with the requested fields stored in this entity, indexed by field name
extractOriginal()  : array<string|int, mixed>
Returns an array with the requested original fields stored in this entity, indexed by field name.
extractOriginalChanged()  : array<string|int, mixed>
Returns an array with only the original fields stored in this entity, indexed by field name.
get()  : mixed
Returns the value of a field by name
getAccessible()  : array<string|int, bool>
Returns the raw accessible configuration for this entity.
getDirty()  : array<string|int, string>
Gets the dirty fields.
getError()  : array<string|int, mixed>
Returns validation errors of a field
getErrors()  : array<string|int, mixed>
Returns all validation errors.
getHidden()  : array<string|int, string>
Gets the hidden fields.
getInvalid()  : array<string, mixed>
Get a list of invalid fields and their data for errors upon validation/patching
getInvalidField()  : mixed|null
Get a single value of an invalid field. Returns null if not set.
getOriginal()  : mixed
Returns the value of an original field by name
getOriginalValues()  : array<string|int, mixed>
Gets all original values of the entity.
getSource()  : string
Returns the alias of the repository from which this entity came from.
getVirtual()  : array<string|int, string>
Gets the virtual fields on this entity.
getVisible()  : array<string|int, string>
Gets the list of visible fields.
has()  : bool
Returns whether this entity contains a field named $field that contains a non-null value.
hasErrors()  : bool
Returns whether this entity has errors.
hasValue()  : bool
Checks that a field has a value.
isAccessible()  : bool
Checks if a field is accessible
isDirty()  : bool
Checks if the entity is dirty or if a single field of it is dirty.
isEmpty()  : bool
Checks that a field is empty
isNew()  : bool
Returns whether this entity has already been persisted.
jsonSerialize()  : array<string|int, mixed>
Returns the fields that will be serialized as JSON
offsetExists()  : bool
Implements isset($entity);
offsetGet()  : mixed
Implements $entity[$offset];
offsetSet()  : void
Implements $entity[$offset] = $value;
offsetUnset()  : void
Implements unset($result[$offset]);
set()  : $this
Sets a single field inside this entity.
setAccess()  : $this
Stores whether a field value can be changed or set in this entity.
setDirty()  : $this
Sets the dirty status of a single field.
setError()  : $this
Sets errors for a single field
setErrors()  : $this
Sets error messages to the entity
setHidden()  : $this
Sets hidden fields.
setInvalid()  : $this
Set fields as invalid and not patchable into the entity.
setInvalidField()  : $this
Sets a field as invalid and not patchable into the entity.
setNew()  : $this
Set the status of this entity.
setSource()  : $this
Sets the source alias
setVirtual()  : $this
Sets the virtual fields on this entity.
toArray()  : array<string|int, mixed>
Returns an array with all the fields that have been set to this entity
unset()  : $this
Removes a field or list of fields from this entity
unsetProperty()  : $this
Removes a field or list of fields from this entity
_accessor()  : string
Fetch accessor method name Accessor methods (available or not) are cached in $_accessors
_nestedErrors()  : array<string|int, mixed>
Auxiliary method for getting errors in nested entities
_readError()  : array<string|int, mixed>
Read the error(s) from one or many objects.
_readHasErrors()  : bool
Reads if there are errors for one or many objects.

Properties

$name

public string $name

$student_id

public int $student_id

$_accessible

Map of fields in this entity that can be safely assigned, each field name points to a boolean indicating its status. An empty array means no fields are accessible

protected array<string, bool> $_accessible = ['*' => true]

The special field '*' can also be mapped, meaning that any other field not defined in the map will take its value. For example, '*' => true means that any field not defined in the map will be accessible by default

$_accessors

Holds a cached list of getters/setters per class

protected static array<string, array<string, array<string, string>>> $_accessors = []

$_dirty

Holds a list of the fields that were modified or added after this object was originally created.

protected array<string|int, bool> $_dirty = []

$_errors

List of errors per field as stored in this object.

protected array<string, mixed> $_errors = []

$_fields

Holds all fields and their values for this entity.

protected array<string, mixed> $_fields = []

$_hidden

List of field names that should **not** be included in JSON or Array representations of this Entity.

protected array<string|int, string> $_hidden = []

$_invalid

List of invalid fields and their data for errors upon validation/patching.

protected array<string, mixed> $_invalid = []

$_new

Indicates whether this entity is yet to be persisted.

protected bool $_new = true

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.

$_original

Holds all fields that have been changed and their original values for this entity.

protected array<string, mixed> $_original = []

$_registryAlias

The alias of the repository this entity came from

protected string $_registryAlias = ''

$_virtual

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.

protected array<string|int, string> $_virtual = []

Methods

__construct()

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

public __construct([array<string, mixed> $properties = [] ][, array<string, mixed> $options = [] ]) : mixed
  • 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
$properties : array<string, mixed> = []

hash of properties to set in this entity

$options : array<string, mixed> = []

list of options to use when creating this entity

__debugInfo()

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

public __debugInfo() : array<string, mixed>
Return values
array<string, mixed>

__get()

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

public & __get(string $field) : mixed
Parameters
$field : string

Name of the field to access

__isset()

Returns whether this entity contains a field named $field and is not set to null.

public __isset(string $field) : bool
Parameters
$field : string

The field to check.

Tags
see
Entity::has()
Return values
bool

__set()

Magic setter to add or edit a field in this entity

public __set(string $field, mixed $value) : void
Parameters
$field : string

The name of the field to set

$value : mixed

The value to set to the field

__toString()

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

public __toString() : string
Return values
string

__unset()

Removes a field from this entity

public __unset(string $field) : void
Parameters
$field : string

The field to unset

clean()

Sets the entire entity as clean, which means that it will appear as no fields being modified or added at all. This is an useful call for an initial object hydration

public clean() : void

extract()

Returns an array with the requested fields stored in this entity, indexed by field name

public extract(array<string|int, string> $fields[, bool $onlyDirty = false ]) : array<string|int, mixed>
Parameters
$fields : array<string|int, string>

list of fields to be returned

$onlyDirty : bool = false

Return the requested field only if it is dirty

Return values
array<string|int, mixed>

extractOriginal()

Returns an array with the requested original fields stored in this entity, indexed by field name.

public extractOriginal(array<string|int, string> $fields) : array<string|int, mixed>

Fields that are unchanged from their original value will be included in the return of this method.

Parameters
$fields : array<string|int, string>

List of fields to be returned

Return values
array<string|int, mixed>

extractOriginalChanged()

Returns an array with only the original fields stored in this entity, indexed by field name.

public extractOriginalChanged(array<string|int, string> $fields) : array<string|int, mixed>

This method will only return fields that have been modified since the entity was built. Unchanged fields will be omitted.

Parameters
$fields : array<string|int, string>

List of fields to be returned

Return values
array<string|int, mixed>

get()

Returns the value of a field by name

public & get(string $field) : mixed
Parameters
$field : string

the name of the field to retrieve

Tags
throws
InvalidArgumentException

if an empty field name is passed

getAccessible()

Returns the raw accessible configuration for this entity.

public getAccessible() : array<string|int, bool>

The * wildcard refers to all fields.

Return values
array<string|int, bool>

getDirty()

Gets the dirty fields.

public getDirty() : array<string|int, string>
Return values
array<string|int, string>

getError()

Returns validation errors of a field

public getError(string $field) : array<string|int, mixed>
Parameters
$field : string

Field name to get the errors from

Return values
array<string|int, mixed>

getErrors()

Returns all validation errors.

public getErrors() : array<string|int, mixed>
Return values
array<string|int, mixed>

getHidden()

Gets the hidden fields.

public getHidden() : array<string|int, string>
Return values
array<string|int, string>

getInvalid()

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

public getInvalid() : array<string, mixed>
Return values
array<string, mixed>

getInvalidField()

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

public getInvalidField(string $field) : mixed|null
Parameters
$field : string

The name of the field.

Return values
mixed|null

getOriginal()

Returns the value of an original field by name

public getOriginal(string $field) : mixed
Parameters
$field : string

the name of the field for which original value is retrieved.

Tags
throws
InvalidArgumentException

if an empty field name is passed.

getOriginalValues()

Gets all original values of the entity.

public getOriginalValues() : array<string|int, mixed>
Return values
array<string|int, mixed>

getSource()

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

public getSource() : string
Return values
string

getVirtual()

Gets the virtual fields on this entity.

public getVirtual() : array<string|int, string>
Return values
array<string|int, string>

getVisible()

Gets the list of visible fields.

public getVisible() : array<string|int, string>

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

Return values
array<string|int, string>

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

has()

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

public has(array<string|int, string>|string $field) : bool

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 fields by passing an array:

$entity->has(['name', 'last_name']);

All fields must not be null to get a truthy result.

When checking multiple fields. All fields must not be null in order for true to be returned.

Parameters
$field : array<string|int, string>|string

The field or fields to check.

Return values
bool

hasErrors()

Returns whether this entity has errors.

public hasErrors([bool $includeNested = true ]) : bool
Parameters
$includeNested : bool = true

true will check nested entities for hasErrors()

Return values
bool

hasValue()

Checks that a field has a value.

public hasValue(string $field) : bool

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
$field : string

The field to check.

Return values
bool

isAccessible()

Checks if a field is accessible

public isAccessible(string $field) : bool

Example:

$entity->isAccessible('id'); // Returns whether it can be set or not
Parameters
$field : string

Field name to check

Return values
bool

isDirty()

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

public isDirty([string|null $field = null ]) : bool
Parameters
$field : string|null = null

The field to check the status for. Null for the whole entity.

Return values
bool

Whether the field was changed or not

isEmpty()

Checks that a field is empty

public isEmpty(string $field) : bool

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
$field : string

The field to check.

Return values
bool

isNew()

Returns whether this entity has already been persisted.

public isNew() : bool
Return values
bool

Whether the entity has been persisted.

jsonSerialize()

Returns the fields that will be serialized as JSON

public jsonSerialize() : array<string|int, mixed>
Return values
array<string|int, mixed>

offsetExists()

Implements isset($entity);

public offsetExists(string $offset) : bool
Parameters
$offset : string

The offset to check.

Return values
bool

Success

offsetGet()

Implements $entity[$offset];

public & offsetGet(string $offset) : mixed
Parameters
$offset : string

The offset to get.

Attributes
#[ReturnTypeWillChange]

offsetSet()

Implements $entity[$offset] = $value;

public offsetSet(string $offset, mixed $value) : void
Parameters
$offset : string

The offset to set.

$value : mixed

The value to set.

offsetUnset()

Implements unset($result[$offset]);

public offsetUnset(string $offset) : void
Parameters
$offset : string

The offset to remove.

set()

Sets a single field inside this entity.

public set(array<string, mixed>|string $field[, mixed $value = null ][, array<string, mixed> $options = [] ]) : $this

Example:

$entity->set('name', 'Andrew');

It is also possible to mass-assign multiple fields to this entity with one call by passing a hashed array as fields in the form of field => 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 fields. 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 fields 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' => false]);

You do not need to use the guard option when assigning fields individually:

// No need to use the guard option.
$entity->set('name', 'Andrew');
Parameters
$field : array<string, mixed>|string

the name of field to set or a list of fields with their respective values

$value : mixed = null

The value to set to the field or an array if the first argument is also an array, in which case will be treated as $options

$options : array<string, mixed> = []

Options to be used for setting the field. Allowed option keys are setter and guard

Tags
throws
InvalidArgumentException
Return values
$this

setAccess()

Stores whether a field value can be changed or set in this entity.

public setAccess(array<string|int, string>|string $field, bool $set) : $this

The special field * can also be marked as accessible or protected, meaning that any other field specified before will take its value. For example $entity->setAccess('*', true) means that any field not specified already will be accessible by default.

You can also call this method with an array of fields, 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 fields as accessible
$entity->setAccess('*', false); // Mark all fields as protected
Parameters
$field : array<string|int, string>|string

Single or list of fields to change its accessibility

$set : bool

True marks the field as accessible, false will mark it as protected.

Return values
$this

setDirty()

Sets the dirty status of a single field.

public setDirty(string $field[, bool $isDirty = true ]) : $this
Parameters
$field : string

the field to set or check status for

$isDirty : bool = true

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

Return values
$this

setError()

Sets errors for a single field

public setError(string $field, array<string|int, mixed>|string $errors[, bool $overwrite = false ]) : $this

Example

// Sets the error messages for a single field
$entity->setError('salary', ['must be numeric', 'must be a positive number']);
Parameters
$field : string

The field to get errors for, or the array of errors to set.

$errors : array<string|int, mixed>|string

The errors to be set for $field

$overwrite : bool = false

Whether to overwrite pre-existing errors for $field

Return values
$this

setErrors()

Sets error messages to the entity

public setErrors(array<string|int, mixed> $errors[, bool $overwrite = false ]) : $this

Example

// Sets the error messages for multiple fields at once
$entity->setErrors(['salary' => ['message'], 'name' => ['another message']]);
Parameters
$errors : array<string|int, mixed>

The array of errors to set.

$overwrite : bool = false

Whether to overwrite pre-existing errors for $fields

Return values
$this

setHidden()

Sets hidden fields.

public setHidden(array<string|int, string> $fields[, bool $merge = false ]) : $this
Parameters
$fields : array<string|int, string>

An array of fields to hide from array exports.

$merge : bool = false

Merge the new fields with the existing. By default false.

Return values
$this

setInvalid()

Set fields as invalid and not patchable into the entity.

public setInvalid(array<string, mixed> $fields[, bool $overwrite = false ]) : $this

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
$fields : array<string, mixed>

The values to set.

$overwrite : bool = false

Whether to overwrite pre-existing values for $field.

Return values
$this

setInvalidField()

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

public setInvalidField(string $field, mixed $value) : $this
Parameters
$field : string

The value to set.

$value : mixed

The invalid value to be set for $field.

Return values
$this

setNew()

Set the status of this entity.

public setNew(bool $new) : $this

Using true means that the entity has not been persisted in the database, false that it already is.

Parameters
$new : bool

Indicate whether this entity has been persisted.

Return values
$this

setSource()

Sets the source alias

public setSource(string $alias) : $this
Parameters
$alias : string

the alias of the repository

Return values
$this

setVirtual()

Sets the virtual fields on this entity.

public setVirtual(array<string|int, string> $fields[, bool $merge = false ]) : $this
Parameters
$fields : array<string|int, string>

An array of fields to treat as virtual.

$merge : bool = false

Merge the new fields with the existing. By default false.

Return values
$this

toArray()

Returns an array with all the fields that have been set to this entity

public toArray() : array<string|int, mixed>

This method will recursively transform entities assigned to fields into arrays as well.

Return values
array<string|int, mixed>

unset()

Removes a field or list of fields from this entity

public unset(array<string|int, string>|string $field) : $this

Examples:

$entity->unset('name');
$entity->unset(['name', 'last_name']);
Parameters
$field : array<string|int, string>|string

The field to unset.

Return values
$this

unsetProperty()

Removes a field or list of fields from this entity

public unsetProperty(array<string|int, string>|string $field) : $this

Use instead. Will be removed in 5.0.

Parameters
$field : array<string|int, string>|string

The field to unset.

Return values
$this

_accessor()

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

protected static _accessor(string $property, string $type) : string
Parameters
$property : string

the field name to derive getter name from

$type : string

the accessor type ('get' or 'set')

Return values
string

method name or empty string (no method available)

_nestedErrors()

Auxiliary method for getting errors in nested entities

protected _nestedErrors(string $field) : array<string|int, mixed>
Parameters
$field : string

the field in this entity to check for errors

Return values
array<string|int, mixed>

errors in nested entity if any

_readError()

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

protected _readError(EntityInterface|iterable<string|int, mixed> $object[, string|null $path = null ]) : array<string|int, mixed>
Parameters
$object : EntityInterface|iterable<string|int, mixed>

The object to read errors from.

$path : string|null = null

The field name for errors.

Return values
array<string|int, mixed>

_readHasErrors()

Reads if there are errors for one or many objects.

protected _readHasErrors(EntityInterface|array<string|int, mixed> $object) : bool
Parameters
$object : EntityInterface|array<string|int, mixed>

The object to read errors from.

Return values
bool

        
On this page

Search results