CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 6.0 API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 6.0
      • 6.0
      • 5.4
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 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

  • Global
  • Cake
    • AttributeResolver
    • Cache
    • Collection
    • Command
    • Console
    • Container
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Lock
    • Log
    • Mailer
    • Network
    • ORM
      • Association
      • Attribute
      • Behavior
      • Exception
      • Locator
      • Query
      • Rule
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

Class Marshaller

Contains logic to convert array data into entities.

Useful when converting request data into entities.

Namespace: Cake\ORM
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 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
  • 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 requireFieldPresence is 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 $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']
      ]
    ]);
  • 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 $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.
  • 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 associated option.

  • 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 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']]]
    ]);
  • 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
BadMethodCallException

InvalidArgumentException

RuntimeException

buildPropertyMap() ¶ 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
InvalidArgumentException
When 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
void

extractAssociations() ¶ 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
array

An 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
mixed

loadAssociatedByIds() ¶ 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\Table::newEntities()
\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>|null

merge() ¶ 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
TMergedEntity
See Also
\Cake\ORM\Entity::$patchable

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>|null

mergeBelongsToMany() ¶ 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
\Cake\ORM\Entity::$patchable

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
array

An 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
TEntity
See Also
\Cake\ORM\Table::newEntity()
\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
array

An 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 true for default validator.

bool $isNew

Whether it is a new entity or one to be updated.

array<string, mixed> $context optional

Additional validation context.

Returns
array

The list of validation errors.

Throws
RuntimeException
If 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>
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs