Class Marshaller
Contains logic to convert array data into entities.
Useful when converting request data into entities.
See: \Cake\ORM\Table::newEntity()
See: \Cake\ORM\Table::newEntities()
See: \Cake\ORM\Table::patchEntity()
See: \Cake\ORM\Table::patchEntities()
Property Summary
-
$associationOptions protected
array<string, int>List of option names accepted inside an association's options array.
Unknown string keys are treated as nested association aliases so that contain-style associated arrays also work with lowercase aliases.
Method Summary
-
__construct() public
Constructor.
-
belongsToMany() protected
Marshals data for belongsToMany associations.
Builds the related entities and handles the special casing for junction table entities.
-
buildPropertyMap() protected
Build the map of property => marshaling callable.
-
dispatchAfterMarshal() protected
dispatch Model.afterMarshal event.
-
extractAssociations() protected
Extracts association names from options array, separating them from actual options.
This allows a contain-style nested array format in the shared
associatedoption:- ['Users', 'Comments'] contains nested associations
- ['Users' => [...], 'Comments'] contains nested associations
- ['onlyIds' => true, 'validate' => false] contains options
- ['Users', 'onlyIds' => true] contains associations and options
-
fieldValue() protected
Get the value of a field from an entity.
It checks whether the field exists in the entity before getting the value to avoid MissingPropertyException if
requireFieldPresenceis enabled. -
loadAssociatedByIds() protected
Loads a list of belongs to many from ids.
-
many() public
Hydrate many entities and their associated data.
Options:
- validate: Set to false to disable validation. Can also be a string of the validator ruleset to be applied. Defaults to true/default.
- associated: Associations listed here will be marshaled as well. Defaults to null.
- fields: An allowed list of fields to be assigned to the entity. If not present, the patchable fields list in the entity will be used. Defaults to null.
- patchableFields: A list of fields to allow or deny in entity patchable fields. Defaults to null
- forceNew: When enabled, belongsToMany associations will have 'new' entities created when primary key values are set, and a record does not already exist. Normally primary key on missing entities would be ignored. Defaults to false.
-
marshalAssociation() protected
Create a new sub-marshaller and marshal the associated data.
-
merge() public
Merges
$datainto$entityand recursively does the same for each one of the association names passed in$options. When merging associations, if an entity is not present in the parent entity for a given association, a new one will be created.When merging HasMany or BelongsToMany associations, all the entities in the
$dataarray will appear, those that can be matched by primary key will get the data merged, but those that cannot, will be discarded.idsoption can be used to determine whether the association must use the_idsformat.Options:
- associated: Associations listed here will be marshaled as well.
- validate: Whether to validate data before hydrating the entities. Can also be set to a string to use a specific validator. Defaults to true/default.
- fields: An allowed list of fields to be assigned to the entity. If not present the patchable fields list in the entity will be used.
- patchableFields: A list of fields to allow or deny in entity patchable fields.
The above options can be used in each nested
associatedarray. In addition to the above options you can also use theonlyIdsoption for HasMany and BelongsToMany associations. When true this option restricts the request data to only be read from_ids.$result = $marshaller->merge($entity, $data, [ 'associated' => ['Tags' => ['onlyIds' => true]] ]);$result = $marshaller->merge($entity, $data, [ 'associated' => [ 'Tags' => ['DeeperAssoc1', 'DeeperAssoc2'] ] ]); -
mergeAssociation() protected
Creates a new sub-marshaller and merges the associated data.
-
mergeBelongsToMany() protected
Creates a new sub-marshaller and merges the associated data for a BelongstoMany association.
-
mergeJoinData() protected
Merge the special junction property (_joinData) into the entity set.
-
mergeMany() public
Merges each of the elements from
$datainto each of the entities in$entitiesand recursively does the same for each of the association names passed in$options. When merging associations, if an entity is not present in the parent entity for a given association, a new one will be created.Records in
$dataare matched against the entities using the primary key column. Entries in$entitiesthat cannot be matched to any record in$datawill be discarded. Records in$datathat could not be matched will be marshaled as a new entity.When merging HasMany or BelongsToMany associations, all the entities in the
$dataarray will appear, those that can be matched by primary key will get the data merged, but those that cannot, will be discarded.Options:
- validate: Whether to validate data before hydrating the entities. Can also be set to a string to use a specific validator. Defaults to true/default.
- associated: Associations listed here will be marshaled as well.
- fields: An allowed list of fields to be assigned to the entity. If not present, the patchable fields list in the entity will be used.
- patchableFields: A list of fields to allow or deny in entity patchable fields.
-
normalizeAssociations() protected
Returns an array out of the original passed associations list where dot notation is transformed into nested arrays so that they can be parsed by other routines.
This method now supports the same nested array format as contain(), allowing:
- Dot notation: ['First.Second']
- Nested arrays: ['First' => ['Second', 'Third']]
- Mixed with options: ['First' => ['Second', 'onlyIds' => true]]
Known option names are parsed as options. Unknown string keys are parsed as association aliases. If an association alias collides with an option name, use dot notation or the explicit
associatedoption. -
one() public
Hydrate one entity and its associated data.
Options:
- validate: Set to false to disable validation. Can also be a string of the validator ruleset to be applied. Defaults to true/default.
- associated: Associations listed here will be marshaled as well. Defaults to null.
- fields: An allowed list of fields to be assigned to the entity. If not present, the patchable fields list in the entity will be used. Defaults to null.
- patchableFields: A list of fields to allow or deny in entity patchable fields. Defaults to null
- forceNew: When enabled, belongsToMany associations will have 'new' entities created when primary key values are set, and a record does not already exist. Normally primary key on missing entities would be ignored. Defaults to false.
The above options can be used in each nested
associatedarray. In addition to the above options you can also use theonlyIdsoption for HasMany and BelongsToMany associations. When true this option restricts the request data to only be read from_ids.$result = $marshaller->one($data, [ 'associated' => ['Tags' => ['onlyIds' => true]] ]);$result = $marshaller->one($data, [ 'associated' => [ 'Tags' => ['patchableFields' => ['*' => true]] ] ]);$result = $marshaller->one($data, [ 'associated' => [ 'Tags' => ['DeeperAssoc1', 'DeeperAssoc2'] ] ]);$result = $marshaller->one($data, [ 'associated' => ['Tags' => ['_joinData' => ['Users']]] ]); -
prepareDataAndOptions() protected
Returns data and options prepared to validate and marshall.
-
validate() protected
Returns the validation errors for a data set based on the passed options
Method Detail
__construct() ¶ public
__construct(Cake\ORM\Table<array<string, \Cake\ORM\Behavior>, TEntity> $table)
Constructor.
Parameters
-
Cake\ORM\Table<array<string, \Cake\ORM\Behavior>, TEntity>$table The table this marshaller is for.
belongsToMany() ¶ protected
belongsToMany(Cake\ORM\Association\BelongsToMany<Cake\ORM\Table> $assoc, array $data, array<string, mixed> $options = []): array<Cake\Datasource\EntityInterface>
Marshals data for belongsToMany associations.
Builds the related entities and handles the special casing for junction table entities.
Parameters
-
Cake\ORM\Association\BelongsToMany<Cake\ORM\Table>$assoc The association to marshal.
-
array$data The data to convert into entities.
-
array<string, mixed>$options optional List of options.
Returns
array<Cake\Datasource\EntityInterface>An array of built entities.
Throws
BadMethodCallExceptionInvalidArgumentExceptionRuntimeExceptionbuildPropertyMap() ¶ protected
buildPropertyMap(array $data, array<string, mixed> $options): array<string, callable>
Build the map of property => marshaling callable.
Parameters
-
array$data The data being marshaled.
-
array<string, mixed>$options List of options containing the 'associated' key.
Returns
array<string, callable>Map of property names to marshaling callables. Each callable accepts the value and entity, and returns the marshaled result.
Throws
InvalidArgumentExceptionWhen associations do not exist.
dispatchAfterMarshal() ¶ protected
dispatchAfterMarshal(Cake\Datasource\EntityInterface $entity, array $data, array<string, mixed> $options = []): void
dispatch Model.afterMarshal event.
Parameters
-
Cake\Datasource\EntityInterface$entity The entity that was marshaled.
-
array$data readOnly $data to use.
-
array<string, mixed>$options optional List of options that are readOnly.
Returns
voidextractAssociations() ¶ protected
extractAssociations(array $options): array
Extracts association names from options array, separating them from actual options.
This allows a contain-style nested array format in the shared associated
option:
- ['Users', 'Comments'] contains nested associations
- ['Users' => [...], 'Comments'] contains nested associations
- ['onlyIds' => true, 'validate' => false] contains options
- ['Users', 'onlyIds' => true] contains associations and options
Parameters
-
array$options The options array that may contain nested associations.
Returns
arrayAn array with two elements: [associations, options]
fieldValue() ¶ protected
fieldValue(Cake\Datasource\EntityInterface $entity, string $field): mixed
Get the value of a field from an entity.
It checks whether the field exists in the entity before getting the value
to avoid MissingPropertyException if requireFieldPresence is enabled.
Parameters
-
Cake\Datasource\EntityInterface$entity The entity to extract the field from.
-
string$field The field to extract.
Returns
mixedloadAssociatedByIds() ¶ protected
loadAssociatedByIds(Cake\ORM\Association $assoc, array $ids): array<Cake\Datasource\EntityInterface>
Loads a list of belongs to many from ids.
Parameters
-
Cake\ORM\Association$assoc The association class for the belongsToMany association.
-
array$ids The list of ids to load.
Returns
array<Cake\Datasource\EntityInterface>An array of entities.
many() ¶ public
many(array $data, array<string, mixed> $options = []): array<TEntity>
Hydrate many entities and their associated data.
Options:
- validate: Set to false to disable validation. Can also be a string of the validator ruleset to be applied. Defaults to true/default.
- associated: Associations listed here will be marshaled as well. Defaults to null.
- fields: An allowed list of fields to be assigned to the entity. If not present, the patchable fields list in the entity will be used. Defaults to null.
- patchableFields: A list of fields to allow or deny in entity patchable fields. Defaults to null
- forceNew: When enabled, belongsToMany associations will have 'new' entities created when primary key values are set, and a record does not already exist. Normally primary key on missing entities would be ignored. Defaults to false.
Parameters
-
array$data The data to hydrate.
-
array<string, mixed>$options optional List of options
Returns
array<TEntity>An array of hydrated records.
See Also
\Cake\ORM\Entity::$patchable
marshalAssociation() ¶ protected
marshalAssociation(Cake\ORM\Association $assoc, mixed $value, array<string, mixed> $options): Cake\Datasource\EntityInterface|array<Cake\Datasource\EntityInterface>|null
Create a new sub-marshaller and marshal the associated data.
Parameters
-
Cake\ORM\Association$assoc The association to marshall
-
mixed$value The data to hydrate. If not an array, this method will return null.
-
array<string, mixed>$options List of options.
Returns
Cake\Datasource\EntityInterface|array<Cake\Datasource\EntityInterface>|nullmerge() ¶ public
merge(TMergedEntity $entity, array $data, array<string, mixed> $options = []): TMergedEntity
Merges $data into $entity and recursively does the same for each one of
the association names passed in $options. When merging associations, if an
entity is not present in the parent entity for a given association, a new one
will be created.
When merging HasMany or BelongsToMany associations, all the entities in the
$data array will appear, those that can be matched by primary key will get
the data merged, but those that cannot, will be discarded. ids option can be used
to determine whether the association must use the _ids format.
Options:
- associated: Associations listed here will be marshaled as well.
- validate: Whether to validate data before hydrating the entities. Can also be set to a string to use a specific validator. Defaults to true/default.
- fields: An allowed list of fields to be assigned to the entity. If not present the patchable fields list in the entity will be used.
- patchableFields: A list of fields to allow or deny in entity patchable fields.
The above options can be used in each nested associated array. In addition to the above
options you can also use the onlyIds option for HasMany and BelongsToMany associations.
When true this option restricts the request data to only be read from _ids.
$result = $marshaller->merge($entity, $data, [
'associated' => ['Tags' => ['onlyIds' => true]]
]);
$result = $marshaller->merge($entity, $data, [
'associated' => [
'Tags' => ['DeeperAssoc1', 'DeeperAssoc2']
]
]);
Templates
TMergedEntity
of \Cake\Datasource\EntityInterface Parameters
-
TMergedEntity$entity the entity that will get the data merged in
-
array$data key value list of fields to be merged into the entity
-
array<string, mixed>$options optional List of options.
Returns
TMergedEntitySee Also
mergeAssociation() ¶ protected
mergeAssociation(Cake\Datasource\EntityInterface|non-empty-array<Cake\Datasource\EntityInterface>|null $original, Cake\ORM\Association $assoc, mixed $value, array<string, mixed> $options): Cake\Datasource\EntityInterface|array<Cake\Datasource\EntityInterface>|null
Creates a new sub-marshaller and merges the associated data.
Parameters
-
Cake\Datasource\EntityInterface|non-empty-array<Cake\Datasource\EntityInterface>|null$original The original entity
-
Cake\ORM\Association$assoc The association to merge
-
mixed$value The array of data to hydrate. If not an array, this method will return null.
-
array<string, mixed>$options List of options.
Returns
Cake\Datasource\EntityInterface|array<Cake\Datasource\EntityInterface>|nullmergeBelongsToMany() ¶ protected
mergeBelongsToMany(array<Cake\Datasource\EntityInterface> $original, Cake\ORM\Association\BelongsToMany<Cake\ORM\Table> $assoc, array $value, array<string, mixed> $options): array<Cake\Datasource\EntityInterface>
Creates a new sub-marshaller and merges the associated data for a BelongstoMany association.
Parameters
-
array<Cake\Datasource\EntityInterface>$original The original entities list.
-
Cake\ORM\Association\BelongsToMany<Cake\ORM\Table>$assoc The association to marshall
-
array$value The data to hydrate
-
array<string, mixed>$options List of options.
Returns
array<Cake\Datasource\EntityInterface>mergeJoinData() ¶ protected
mergeJoinData(array<Cake\Datasource\EntityInterface> $original, Cake\ORM\Association\BelongsToMany<Cake\ORM\Table> $assoc, array $value, array<string, mixed> $options): array<Cake\Datasource\EntityInterface>
Merge the special junction property (_joinData) into the entity set.
Parameters
-
array<Cake\Datasource\EntityInterface>$original The original entities list.
-
Cake\ORM\Association\BelongsToMany<Cake\ORM\Table>$assoc The association to marshall
-
array$value The data to hydrate
-
array<string, mixed>$options List of options.
Returns
array<Cake\Datasource\EntityInterface>An array of entities
mergeMany() ¶ public
mergeMany(iterable<TMergedEntity> $entities, array $data, array<string, mixed> $options = []): array<TEntity|TMergedEntity>
Merges each of the elements from $data into each of the entities in $entities
and recursively does the same for each of the association names passed in
$options. When merging associations, if an entity is not present in the parent
entity for a given association, a new one will be created.
Records in $data are matched against the entities using the primary key
column. Entries in $entities that cannot be matched to any record in
$data will be discarded. Records in $data that could not be matched will
be marshaled as a new entity.
When merging HasMany or BelongsToMany associations, all the entities in the
$data array will appear, those that can be matched by primary key will get
the data merged, but those that cannot, will be discarded.
Options:
- validate: Whether to validate data before hydrating the entities. Can also be set to a string to use a specific validator. Defaults to true/default.
- associated: Associations listed here will be marshaled as well.
- fields: An allowed list of fields to be assigned to the entity. If not present, the patchable fields list in the entity will be used.
- patchableFields: A list of fields to allow or deny in entity patchable fields.
Templates
TMergedEntity
of \Cake\Datasource\EntityInterface Parameters
-
iterable<TMergedEntity>$entities the entities that will get the data merged in
-
array$data list of arrays to be merged into the entities
-
array<string, mixed>$options optional List of options.
Returns
array<TEntity|TMergedEntity>See Also
normalizeAssociations() ¶ protected
normalizeAssociations(array|string $associations): array
Returns an array out of the original passed associations list where dot notation is transformed into nested arrays so that they can be parsed by other routines.
This method now supports the same nested array format as contain(), allowing:
- Dot notation: ['First.Second']
- Nested arrays: ['First' => ['Second', 'Third']]
- Mixed with options: ['First' => ['Second', 'onlyIds' => true]]
Known option names are parsed as options. Unknown string keys are parsed as
association aliases. If an association alias collides with an option name,
use dot notation or the explicit associated option.
Parameters
-
array|string$associations The array of included associations.
Returns
arrayAn array having dot notation transformed into nested arrays
one() ¶ public
one(array<string, mixed> $data, array<string, mixed> $options = []): TEntity
Hydrate one entity and its associated data.
Options:
- validate: Set to false to disable validation. Can also be a string of the validator ruleset to be applied. Defaults to true/default.
- associated: Associations listed here will be marshaled as well. Defaults to null.
- fields: An allowed list of fields to be assigned to the entity. If not present, the patchable fields list in the entity will be used. Defaults to null.
- patchableFields: A list of fields to allow or deny in entity patchable fields. Defaults to null
- forceNew: When enabled, belongsToMany associations will have 'new' entities created when primary key values are set, and a record does not already exist. Normally primary key on missing entities would be ignored. Defaults to false.
The above options can be used in each nested associated array. In addition to the above
options you can also use the onlyIds option for HasMany and BelongsToMany associations.
When true this option restricts the request data to only be read from _ids.
$result = $marshaller->one($data, [
'associated' => ['Tags' => ['onlyIds' => true]]
]);
$result = $marshaller->one($data, [
'associated' => [
'Tags' => ['patchableFields' => ['*' => true]]
]
]);
$result = $marshaller->one($data, [
'associated' => [
'Tags' => ['DeeperAssoc1', 'DeeperAssoc2']
]
]);
$result = $marshaller->one($data, [
'associated' => ['Tags' => ['_joinData' => ['Users']]]
]);
Parameters
-
array<string, mixed>$data The data to hydrate.
-
array<string, mixed>$options optional List of options
Returns
TEntitySee Also
\Cake\ORM\Entity::$patchable
prepareDataAndOptions() ¶ protected
prepareDataAndOptions(array<string, mixed> $data, array<string, mixed> $options): array
Returns data and options prepared to validate and marshall.
Parameters
-
array<string, mixed>$data The data to prepare.
-
array<string, mixed>$options The options passed to this marshaller.
Returns
arrayAn array containing prepared data and options.
validate() ¶ protected
validate(array $data, string|bool $validator, bool $isNew, array<string, mixed> $context = []): array
Returns the validation errors for a data set based on the passed options
Parameters
-
array$data The data to validate.
-
string|bool$validator Validator name or
truefor default validator.-
bool$isNew Whether it is a new entity or one to be updated.
-
array<string, mixed>$context optional Additional validation context.
Returns
arrayThe list of validation errors.
Throws
RuntimeExceptionIf no validator can be created.
Property Detail
$associationOptions ¶ protected
List of option names accepted inside an association's options array.
Unknown string keys are treated as nested association aliases so that contain-style associated arrays also work with lowercase aliases.
Type
array<string, int>