Provides a form context around a single entity and its relations.
It also can be used as context around an array or iterator of entities.
This class lets FormHelper interface with entities or collections of entities.
Important Keys:
entity
The entity this context is operating on.table
Either the ORM\Table instance to fetch schema/validators from, an array of table instances in the case of a form spanning multiple entities, or the name(s) of the table. If this is null the table name(s) will be determined using naming conventions.validator
Either the Validation\Validator to use, or the name of the validation method to call on the table object. For example 'default'. Defaults to 'default'. Can be an array of table alias=>validators when dealing with associated forms.array
Context data for this object.
bool
Boolean to track whether or not the entity is a collection.
string
The name of the top level entity/table object.
\Cake\ORM\Locator\LocatorInterface|null
Table locator instance
\Cake\ORM\Table[]
A dictionary of tables
\Cake\Validation\Validator[]
Dictionary of validators.
Helper method used to extract all the primary key values out of an array, The primary key column is guessed out of the provided $path array
Returns true if the passed field name is part of the primary key for this context
__construct(array $context)
Constructor.
array
$context Context info.
_extractMultiple(mixed $values, array $path)
Helper method used to extract all the primary key values out of an array, The primary key column is guessed out of the provided $path array
mixed
$values The list from which to extract primary keys from
string[]
$path Each one of the parts in a path for a field name
array|null
_getProp(mixed $target, mixed $field)
Read property values or traverse arrays/iterators.
mixed
$target The entity/array/collection to fetch $field from.
string
$field The next field to fetch.
mixed
_getTable(mixed $parts, mixed $fallback)
Get the table instance from a property path
string[]|string|\Cake\Datasource\EntityInterface
$parts Each one of the parts in a path for a field name
bool
$fallback optional Whether or not to fallback to the last found table when a non-existent field/property is being encountered.
\Cake\ORM\Table|null
Table instance or null
_getValidator(array $parts)
Get the validator associated to an entity based on naming conventions.
array
$parts Each one of the parts in a path for a field name
\Cake\Validation\Validator
RuntimeException
_prepare()
Prepare some additional data from the context.
If the table option was provided to the constructor and it was a string, TableLocator will be used to get the correct table instance.
If an object is provided as the table option, it will be used as is.
If no table option is provided, the table name will be derived based on naming conventions. This inference will work with a number of common objects like arrays, Collection objects and ResultSets.
RuntimeException
_schemaDefault(array $parts)
Get default value from table schema for given entity field.
string[]
$parts Each one of the parts in a path for a field name
mixed
attributes(string $field)
Get an associative array of other attributes for a field name.
string
$field A dot separated path to get additional data on.
array
An array of data describing the additional attributes on a field.
entity(?array $path)
Fetch the entity or data value for a given path
This method will traverse the given path and find the entity or array value for a given path.
If you only want the terminal Entity for a path use leafEntity
instead.
array|null
$path optional Each one of the parts in a path for a field name or null to get the entity passed in constructor context.
\Cake\Datasource\EntityInterface|iterable|null
RuntimeException
error(string $field)
Get the errors for a given field
string
$field A dot separated path to check errors on.
array
An array of errors.
fieldNames()
Get the field names from the top level entity.
If the context is for an array of entities, the 0th index will be used.
string[]
Array of field names in the table/entity.
getMaxLength(string $field)
Get field length from validation
string
$field The dot separated path to the field you want to check.
int|null
getPrimaryKey()
Get the primary key data for the context.
Gets the primary key columns from the root entity's schema.
string[]
getRequiredMessage(string $field)
Gets the default "required" error message for a field
string
$field A dot separated path to the field name
string|null
getTableLocator()
Gets the table locator.
\Cake\ORM\Locator\LocatorInterface
hasError(string $field)
Check whether or not a field has an error attached to it
string
$field A dot separated path to check errors on.
bool
Returns true if the errors for the field are not empty.
isCreate()
Check whether or not this form is a create or update.
If the context is for a single entity, the entity's isNew() method will be used. If isNew() returns null, a create operation will be assumed.
If the context is for a collection or array the first object in the collection will be used.
bool
isPrimaryKey(string $field)
Returns true if the passed field name is part of the primary key for this context
string
$field A dot separated path to the field a value is needed for.
bool
isRequired(string $field)
Check if a field should be marked as required.
string
$field The dot separated path to the field you want to check.
bool|null
leafEntity(mixed $path)
Fetch the terminal or leaf entity for the given path.
Traverse the path until an entity cannot be found. Lists containing entities will be traversed if the first element contains an entity. Otherwise the containing Entity will be assumed to be the terminal one.
array|null
$path optional Each one of the parts in a path for a field name or null to get the entity passed in constructor context.
array
Containing the found entity, and remaining un-matched path.
RuntimeException
primaryKey()
Get the primary key data for the context.
Gets the primary key columns from the root entity's schema.
string[]
setTableLocator(\Cake\ORM\Locator\LocatorInterface $tableLocator)
Sets the table locator.
\Cake\ORM\Locator\LocatorInterface
$tableLocator LocatorInterface instance.
$this
type(string $field)
Get the abstract field type for a given field name.
string
$field A dot separated path to get a schema type for.
string|null
An abstract data type or null.
val(string $field, array $options)
Get the value for a given path.
Traverses the entity data and finds the value for $path.
string
$field The dot separated path to the value.
array
$options optional Options:
default
: Default value to return if no value found in data or entity.schemaDefault
: Boolean indicating whether default value from table schema should be used if it's not explicitly provided.mixed
The value of the field or null on a miss.
Context data for this object.
array
Boolean to track whether or not the entity is a collection.
bool
The name of the top level entity/table object.
string
Table locator instance
\Cake\ORM\Locator\LocatorInterface|null
A dictionary of tables
\Cake\ORM\Table[]
Dictionary of validators.
\Cake\Validation\Validator[]
© 2005–present 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.
https://api.cakephp.org/4.1/class-Cake.View.Form.EntityContext.html