Class EntityContext
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:
- entityThe entity this context is operating on.
- tableEither 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.
- validatorEither 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.
Constants
- 
          
          array<string>VALID_ATTRIBUTES ¶['length', 'precision', 'comment', 'null', 'default']
Property Summary
- 
        $_context protectedarray<string, mixed>Context data for this object. 
- 
        $_isCollection protectedboolBoolean to track whether the entity is a collection. 
- 
        $_rootName protectedstringThe name of the top level entity/table object. 
- 
        $_tableLocator protectedCake\ORM\Locator\LocatorInterface|nullTable locator instance 
- 
        $_tables protectedarray<Cake\ORM\Table>A dictionary of tables 
- 
        $_validator protectedarray<Cake\Validation\Validator>Dictionary of validators. 
- 
        $defaultTable protectedstring|nullThis object's default table alias. 
Method Summary
- 
          __construct() publicConstructor. 
- 
          _extractMultiple() protectedHelper 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 
- 
          _getProp() protectedRead property values or traverse arrays/iterators. 
- 
          _getTable() protectedGet the table instance from a property path 
- 
          _getValidator() protectedGet the validator associated to an entity based on naming conventions. 
- 
          _prepare() protectedPrepare some additional data from the context. 
- 
          _schemaDefault() protectedGet default value from table schema for given entity field. 
- 
          attributes() publicGet an associative array of other attributes for a field name. 
- 
          entity() publicFetch the entity or data value for a given path 
- 
          error() publicGet the errors for a given field 
- 
          fetchTable() publicConvenience method to get a table instance. 
- 
          fieldNames() publicGet the field names from the top level entity. 
- 
          getMaxLength() publicGet field length from validation 
- 
          getPrimaryKey() publicGet the primary key data for the context. 
- 
          getRequiredMessage() publicGets the default "required" error message for a field 
- 
          getTableLocator() publicGets the table locator. 
- 
          hasError() publicCheck whether a field has an error attached to it 
- 
          isCreate() publicCheck whether this form is a create or update. 
- 
          isPrimaryKey() publicReturns true if the passed field name is part of the primary key for this context 
- 
          isRequired() publicCheck if a field should be marked as required. 
- 
          leafEntity() protectedFetch the terminal or leaf entity for the given path. 
- 
          primaryKey() public deprecatedGet the primary key data for the context. 
- 
          setTableLocator() publicSets the table locator. 
- 
          type() publicGet the abstract field type for a given field name. 
- 
          val() publicGet the value for a given path. 
Method Detail
__construct() ¶ public
__construct(array<string, mixed> $context)Constructor.
Parameters
- 
                array<string, mixed>$context
- Context info. 
_extractMultiple() ¶ protected
_extractMultiple(mixed $values, array<string> $path): array|nullHelper 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
Parameters
- 
                mixed$values
- The list from which to extract primary keys from 
- 
                array<string>$path
- Each one of the parts in a path for a field name 
Returns
array|null_getProp() ¶ protected
_getProp(mixed $target, string $field): mixedRead property values or traverse arrays/iterators.
Parameters
- 
                mixed$target
- The entity/array/collection to fetch $field from. 
- 
                string$field
- The next field to fetch. 
Returns
mixed_getTable() ¶ protected
_getTable(Cake\Datasource\EntityInterface|array<string>|string $parts, bool $fallback = true): Cake\ORM\Table|nullGet the table instance from a property path
Parameters
- 
                Cake\Datasource\EntityInterface|array<string>|string$parts
- Each one of the parts in a path for a field name 
- 
                bool$fallback optional
- Whether to fallback to the last found table when a nonexistent field/property is being encountered. 
Returns
Cake\ORM\Table|nullTable instance or null
_getValidator() ¶ protected
_getValidator(array $parts): Cake\Validation\ValidatorGet the validator associated to an entity based on naming conventions.
Parameters
- 
                array$parts
- Each one of the parts in a path for a field name 
Returns
Cake\Validation\ValidatorThrows
RuntimeExceptionIf validator cannot be retrieved based on the parts.
_prepare() ¶ protected
_prepare(): voidPrepare 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.
Returns
voidThrows
RuntimeExceptionWhen a table object cannot be located/inferred.
_schemaDefault() ¶ protected
_schemaDefault(array<string> $parts): mixedGet default value from table schema for given entity field.
Parameters
- 
                array<string>$parts
- Each one of the parts in a path for a field name 
Returns
mixedattributes() ¶ public
attributes(string $field): arrayGet an associative array of other attributes for a field name.
Parameters
- 
                string$field
- A dot separated path to get additional data on. 
Returns
arrayAn array of data describing the additional attributes on a field.
entity() ¶ public
entity(array|null $path = null): Cake\Datasource\EntityInterface|iterable|nullFetch 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.
Parameters
- 
                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. 
Returns
Cake\Datasource\EntityInterface|iterable|nullThrows
RuntimeExceptionWhen properties cannot be read.
error() ¶ public
error(string $field): arrayGet the errors for a given field
Parameters
- 
                string$field
- A dot separated path to check errors on. 
Returns
arrayAn array of errors.
fetchTable() ¶ public
fetchTable(string|null $alias = null, array<string, mixed> $options = []): Cake\ORM\TableConvenience method to get a table instance.
Parameters
- 
                string|null$alias optional
- The alias name you want to get. Should be in CamelCase format. If - nullthen the value of $defaultTable property is used.
- 
                array<string, mixed>$options optional
- The options you want to build the table with. If a table has already been loaded the registry options will be ignored. 
Returns
Cake\ORM\TableThrows
Cake\Core\Exception\CakeExceptionIf `$alias` argument and `$defaultTable` property both are `null`.
See Also
fieldNames() ¶ public
fieldNames(): array<string>Get the field names from the top level entity.
If the context is for an array of entities, the 0th index will be used.
Returns
array<string>Array of field names in the table/entity.
getMaxLength() ¶ public
getMaxLength(string $field): int|nullGet field length from validation
Parameters
- 
                string$field
- The dot separated path to the field you want to check. 
Returns
int|nullgetPrimaryKey() ¶ public
getPrimaryKey(): array<string>Get the primary key data for the context.
Gets the primary key columns from the root entity's schema.
Returns
array<string>getRequiredMessage() ¶ public
getRequiredMessage(string $field): string|nullGets the default "required" error message for a field
Parameters
- 
                string$field
Returns
string|nullgetTableLocator() ¶ public
getTableLocator(): Cake\ORM\Locator\LocatorInterfaceGets the table locator.
Returns
Cake\ORM\Locator\LocatorInterfacehasError() ¶ public
hasError(string $field): boolCheck whether a field has an error attached to it
Parameters
- 
                string$field
- A dot separated path to check errors on. 
Returns
boolReturns true if the errors for the field are not empty.
isCreate() ¶ public
isCreate(): boolCheck whether 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.
Returns
boolisPrimaryKey() ¶ public
isPrimaryKey(string $field): boolReturns true if the passed field name is part of the primary key for this context
Parameters
- 
                string$field
Returns
boolisRequired() ¶ public
isRequired(string $field): bool|nullCheck if a field should be marked as required.
In this context class, this is simply defined by the 'required' array.
Parameters
- 
                string$field
- The dot separated path to the field you want to check. 
Returns
bool|nullleafEntity() ¶ protected
leafEntity(array|null $path = null): arrayFetch 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.
Parameters
- 
                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. 
Returns
arrayContaining the found entity, and remaining un-matched path.
Throws
RuntimeExceptionWhen properties cannot be read.
primaryKey() ¶ public
primaryKey(): array<string>Get the primary key data for the context.
Gets the primary key columns from the root entity's schema.
Returns
array<string>setTableLocator() ¶ public
setTableLocator(Cake\ORM\Locator\LocatorInterface $tableLocator): $thisSets the table locator.
Parameters
- 
                Cake\ORM\Locator\LocatorInterface$tableLocator
- LocatorInterface instance. 
Returns
$thistype() ¶ public
type(string $field): string|nullGet the abstract field type for a given field name.
Parameters
- 
                string$field
- A dot separated path to get a schema type for. 
Returns
string|nullAn abstract data type or null.
See Also
val() ¶ public
val(string $field, array<string, mixed> $options = []): mixedGet the value for a given path.
Traverses the entity data and finds the value for $path.
Parameters
- 
                string$field
- The dot separated path to the value. 
- 
                array<string, mixed>$options optional
- Options: 
Returns
mixedThe value of the field or null on a miss.
