Represents an M - N relationship where there exists a junction - or join - table that contains the association fields between the source and the target table.
An example of a BelongsToMany association would be Article belongs to many Tags. In this example 'Article' is the source table and 'Tags' is the target table.
string
'manyToMany'
Association type for many to many associations.
string
'manyToOne'
Association type for many to one associations.
string
'oneToMany'
Association type for one to many associations.
string
'oneToOne'
Association type for one to one associations.
string
'append'
Saving strategy that will only append to the links set
string
'replace'
Saving strategy that will replace the links with the provided set
string
'join'
Strategy name to use joins for fetching associated records
string
'select'
Strategy name to use a select for fetching associated records
string
'subquery'
Strategy name to use a subquery for fetching associated records
array<string>|string|null
The field name in the owning side table that is used to match with the foreignKey
bool
Whether cascaded deletes should also fire callbacks.
string
The class name of the target table object
Closure|array
A list of conditions to be always included when fetching records from the target association
bool
Whether the records on the joint table should be removed when a record on the source table is deleted.
array|string
The default finder name to use for fetching rows from the target table With array value, finder name and default options are allowed.
array<string>|string
The name of the field representing the foreign key to the table to load
string
The type of join to be used when adding the association to a query
string
The name of the hasMany association from the target table to the junction table
array|null
Filtered conditions that reference the junction table.
string
The name of the property to be set containing data from the junction table once a record from the target table is hydrated
Cake\ORM\Table
Junction table instance
string
Junction table name
string
Name given to the association, it usually represents the alias assigned to the target associated table
string
The property name that should be filled with data from the target table in the source table record.
string
Saving strategy to be used by this association
mixed
Order in which target records should be returned
Cake\ORM\Table
Source table instance
string
The strategy name to be used to fetch associated records.
Cake\ORM\Locator\LocatorInterface|null
Table locator instance
array|null
Filtered conditions that reference the target table.
array<string>|string|null
The name of the field representing the foreign key to the target table
Cake\ORM\Table
Target table instance
Cake\ORM\Table|string
The table instance for the junction relation.
array<string>
Valid strategies for this type of association
string|null
This object's default table alias.
Proxies method calls to the target table.
Constructor. Subclasses can override _options function to get the original list of passed options if expecting any other special key
Proxies property retrieval to the target table. This is handy for getting this association's associations
Proxies the isset call to the target table. This is handy to check if the target table has another association with the passed name
Helper function used to conditionally append fields to the select clause of a query from the fields found in another query object.
Append a join to the junction table.
Conditionally adds a condition to the passed Query that will make it find records where there is no match with this association.
Applies all attachable associations to $query
out of the containments found in the $surrogate
query.
Creates a camelized version of $name
Throws an exception should any of the passed entities is not persisted.
Returns the list of joint entities that exist between the source entity and each of the passed target entities
Helper method used to delete the difference between the links passed in $existing
and $jointEntities
. This method will return the values from $targetEntities
that were not deleted from calculating the difference.
Triggers beforeFind on the target table for the query this association is attaching to
Creates the proper entity name (singular) for the specified name
Helper method to infer the requested finder and its options.
Creates a fixture name
Adds a formatter function to the passed $query
if the $surrogate
query declares any other formatter. Since the $surrogate
query correspond to the associated target table, the resulting formatter will be the result of applying the surrogate formatters to only the property corresponding to such table.
Generate associations on the junction table as necessary
Generate additional source table associations as necessary.
Generate reciprocal associations as necessary.
Return false as join conditions are defined in the junction table
Returns the name of the association from the target table to the junction table, this name is used to generate alias in the query and to later on retrieve the results.
Sets the name of the junction table. If no arguments are passed the current configured name is returned. A default name based of the associated tables will be generated if none found.
Creates the proper underscored model key for associations
Creates the proper model name from a foreign key
Parse extra options passed in the constructor.
Return plugin's namespace
Find the correct path for a plugin. Scans $pluginPaths for the plugin you want.
Creates the plural human name used in views
Returns default property name based on association name.
Creates links between the source entity and each of the passed target entities
Persists each of the entities into the target table and creates links between the parent entity and each one of the saved target entities.
Creates the singular human name used in views
Creates the singular name for use in views.
Creates the plural variable name for views
Alters a Query object to include the associated target table data in the final result
Whether this association can be expressed directly in a query join
Clear out the data in the junction table for a given entity.
Returns a modified row after appending a property for this association with the default empty value according to whether the association was joined or fetched externally.
Proxies the delete operation to the target table's deleteAll method
Eager loads a list of records in the target table that are related to another set of records in the source table. Source records can be specified in two ways: first one is by passing a Query object setup to find on the source table and the other way is by explicitly passing an array of primary key values from the source table.
Proxies the operation to the target table's exists method after appending the default conditions for this association
Convenience method to get a table instance.
Proxies the finding operation to the target table's find method and modifies the query accordingly based of this association configuration.
Gets the name of the field representing the binding field with the target table. When not manually specified the primary key of the owning side table is used.
Gets whether cascaded deletes should also fire callbacks.
Gets the class name of the target table object.
Gets a list of conditions to be always included when fetching records from the target association.
Sets whether the records on the target table are dependent on the source table.
Gets the default finder to use for fetching rows from the target table.
Gets the name of the field representing the foreign key to the source table.
Gets the type of join to be used when adding the association to a query.
Gets the name for this association, usually the alias assigned to the target associated table
Gets the property name that should be filled with data from the target table in the source table record.
Gets the strategy that should be used for saving.
Gets the sort order in which target records should be returned.
Gets the table instance for the source side of the association.
Gets the strategy name to be used to fetch associated records. Keep in mind that some association types might not implement but a default strategy, rendering any changes to this setting void.
Gets the table locator.
Gets the table instance for the target side of the association.
Gets the name of the field representing the foreign key to the target table.
Gets the current join table, either the name of the Table instance or the instance itself.
Returns boolean true, as both of the tables 'own' rows in the other side of the association via the joint table.
Sets the table instance for the junction relation. If no arguments are passed, the current configured table instance is returned
Returns filtered conditions that specifically reference the junction table.
Associates the source entity to each of the target entities provided by creating links in the junction table. Both the source entity and each of the target entities are assumed to be already persisted, if they are marked as new or their status is unknown then an exception will be thrown.
Replaces existing association links between the source entity and the target with the ones passed. This method does a smart cleanup, links that are already persisted and present in $targetEntities
will not be deleted, new links will be created for the passed target entities that are not already in the database and the rest will be removed.
Returns true if the eager loading process will require a set of the owning table's binding keys in order to use them as a filter in the finder query.
Takes an entity from the source table and looks if there is a field matching the property name for this association. The found entity will be saved on the target table for this association by passing supplied $options
Sets the name of the field representing the binding field with the target table. When not manually specified the primary key of the owning side table is used.
Sets whether cascaded deletes should also fire callbacks.
Sets the class name of the target table object.
Sets a list of conditions to be always included when fetching records from the target association.
Sets whether the records on the target table are dependent on the source table.
Sets the default finder to use for fetching rows from the target table.
Sets the name of the field representing the foreign key to the target table.
Sets the type of join to be used when adding the association to a query.
Sets the name for this association, usually the alias assigned to the target associated table
Sets the property name that should be filled with data from the target table in the source table record.
Sets the strategy that should be used for saving.
Sets the sort order in which target records should be returned.
Sets the table instance for the source side of the association.
Sets the strategy name to be used to fetch associated records. Keep in mind that some association types might not implement but a default strategy, rendering any changes to this setting void.
Sets the table locator.
Sets the table instance for the target side of the association.
Sets the name of the field representing the foreign key to the target table.
Sets the current join table, either the name of the Table instance or the instance itself.
Returns filtered conditions that reference the target table.
Correctly nests a result row associated values into the correct array keys inside the source results.
Get the relationship type.
Removes all links between the passed source entity and each of the provided target entities. This method assumes that all passed objects are already persisted in the database and that each of them contain a primary key value.
Proxies the update operation to the target table's updateAll method
__call(string $method, array $argument): mixed
Proxies method calls to the target table.
string
$method name of the method to be invoked
array
$argument List of arguments passed to the function
mixed
BadMethodCallException
__construct(string $alias, array<string, mixed> $options = [])
Constructor. Subclasses can override _options function to get the original list of passed options if expecting any other special key
string
$alias The name given to the association
array<string, mixed>
$options optional A list of properties to be set on this object
__get(string $property): Cake\ORM\Association
Proxies property retrieval to the target table. This is handy for getting this association's associations
string
$property the property name
Cake\ORM\Association
RuntimeException
__isset(string $property): bool
Proxies the isset call to the target table. This is handy to check if the target table has another association with the passed name
string
$property the property name
bool
_appendFields(Cake\ORM\Query $query, Cake\ORM\Query $surrogate, array<string, mixed> $options): void
Helper function used to conditionally append fields to the select clause of a query from the fields found in another query object.
Cake\ORM\Query
$query the query that will get the fields appended to
Cake\ORM\Query
$surrogate the query having the fields to be copied from
array<string, mixed>
$options options passed to the method attachTo
void
_appendJunctionJoin(Cake\ORM\Query $query, array|null $conditions = null): Cake\ORM\Query
Append a join to the junction table.
Cake\ORM\Query
$query The query to append.
array|null
$conditions optional The query conditions to use.
Cake\ORM\Query
_appendNotMatching(Cake\ORM\Query $query, array<string, mixed> $options): void
Conditionally adds a condition to the passed Query that will make it find records where there is no match with this association.
Cake\ORM\Query
$query array<string, mixed>
$options void
_bindNewAssociations(Cake\ORM\Query $query, Cake\ORM\Query $surrogate, array<string, mixed> $options): void
Applies all attachable associations to $query
out of the containments found in the $surrogate
query.
Copies all contained associations from the $surrogate
query into the passed $query
. Containments are altered so that they respect the associations chain from which they originated.
Cake\ORM\Query
$query the query that will get the associations attached to
Cake\ORM\Query
$surrogate the query having the containments to be attached
array<string, mixed>
$options options passed to the method attachTo
void
_camelize(string $name): string
Creates a camelized version of $name
string
$name name
string
_checkPersistenceStatus(Cake\Datasource\EntityInterface $sourceEntity, arrayCake\Datasource\EntityInterface> $targetEntities): bool
Throws an exception should any of the passed entities is not persisted.
Cake\Datasource\EntityInterface
$sourceEntity the row belonging to the source
side of this association
arrayCake\Datasource\EntityInterface>
$targetEntities list of entities belonging to the target
side of this association
bool
InvalidArgumentException
_collectJointEntities(Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities): arrayCake\Datasource\EntityInterface>
Returns the list of joint entities that exist between the source entity and each of the passed target entities
Cake\Datasource\EntityInterface
$sourceEntity The row belonging to the source side of this association.
array
$targetEntities The rows belonging to the target side of this association.
arrayCake\Datasource\EntityInterface>
InvalidArgumentException
_diffLinks(Cake\ORM\Query $existing, arrayCake\Datasource\EntityInterface> $jointEntities, array $targetEntities, array<string, mixed> $options = []): array|false
Helper method used to delete the difference between the links passed in $existing
and $jointEntities
. This method will return the values from $targetEntities
that were not deleted from calculating the difference.
Cake\ORM\Query
$existing a query for getting existing links
arrayCake\Datasource\EntityInterface>
$jointEntities link entities that should be persisted
array
$targetEntities entities in target table that are related to the $jointEntities
array<string, mixed>
$options optional list of options accepted by Table::delete()
array|false
_dispatchBeforeFind(Cake\ORM\Query $query): void
Triggers beforeFind on the target table for the query this association is attaching to
Cake\ORM\Query
$query the query this association is attaching itself to
void
_entityName(string $name): string
Creates the proper entity name (singular) for the specified name
string
$name Name
string
_extractFinder(array|string $finderData): array
Helper method to infer the requested finder and its options.
Returns the inferred options from the finder $type.
The following will call the finder 'translations' with the value of the finder as its options: $query->contain(['Comments' => ['finder' => ['translations']]]); $query->contain(['Comments' => ['finder' => ['translations' => []]]]); $query->contain(['Comments' => ['finder' => ['translations' => ['locales' => ['en_US']]]]]);
array|string
$finderData The finder name or an array having the name as key and options as value.
array
_fixtureName(string $name): string
Creates a fixture name
string
$name Model class name
string
_formatAssociationResults(Cake\ORM\Query $query, Cake\ORM\Query $surrogate, array<string, mixed> $options): void
Adds a formatter function to the passed $query
if the $surrogate
query declares any other formatter. Since the $surrogate
query correspond to the associated target table, the resulting formatter will be the result of applying the surrogate formatters to only the property corresponding to such table.
Cake\ORM\Query
$query the query that will get the formatter applied to
Cake\ORM\Query
$surrogate the query having formatters for the associated target table.
array<string, mixed>
$options options passed to the method attachTo
void
_generateJunctionAssociations(Cake\ORM\Table $junction, Cake\ORM\Table $source, Cake\ORM\Table $target): void
Generate associations on the junction table as necessary
Generates the following associations:
You can override these generated associations by defining associations with the correct aliases.
Cake\ORM\Table
$junction The junction table.
Cake\ORM\Table
$source The source table.
Cake\ORM\Table
$target The target table.
void
InvalidArgumentException
_generateSourceAssociations(Cake\ORM\Table $junction, Cake\ORM\Table $source): void
Generate additional source table associations as necessary.
Generates the following associations:
You can override these generated associations by defining associations with the correct aliases.
Cake\ORM\Table
$junction The junction table.
Cake\ORM\Table
$source The source table.
void
_generateTargetAssociations(Cake\ORM\Table $junction, Cake\ORM\Table $source, Cake\ORM\Table $target): void
Generate reciprocal associations as necessary.
Generates the following associations:
You can override these generated associations by defining associations with the correct aliases.
Cake\ORM\Table
$junction The junction table.
Cake\ORM\Table
$source The source table.
Cake\ORM\Table
$target The target table.
void
_joinCondition(array<string, mixed> $options): array
Return false as join conditions are defined in the junction table
array<string, mixed>
$options list of options passed to attachTo method
array
_junctionAssociationName(): string
Returns the name of the association from the target table to the junction table, this name is used to generate alias in the query and to later on retrieve the results.
string
_junctionTableName(string|null $name = null): string
Sets the name of the junction table. If no arguments are passed the current configured name is returned. A default name based of the associated tables will be generated if none found.
string|null
$name optional The name of the junction table.
string
_modelKey(string $name): string
Creates the proper underscored model key for associations
If the input contains a dot, assume that the right side is the real table name.
string
$name Model class name
string
_modelNameFromKey(string $key): string
Creates the proper model name from a foreign key
string
$key Foreign key
string
_options(array<string, mixed> $options): void
Parse extra options passed in the constructor.
array<string, mixed>
$options original list of options passed in constructor
void
_pluginNamespace(string $pluginName): string
Return plugin's namespace
string
$pluginName Plugin name
string
_pluginPath(string $pluginName): string
Find the correct path for a plugin. Scans $pluginPaths for the plugin you want.
string
$pluginName Name of the plugin you want ie. DebugKit
string
_pluralHumanName(string $name): string
Creates the plural human name used in views
string
$name Controller name
string
_propertyName(): string
Returns default property name based on association name.
string
_saveLinks(Cake\Datasource\EntityInterface $sourceEntity, arrayCake\Datasource\EntityInterface> $targetEntities, array<string, mixed> $options): bool
Creates links between the source entity and each of the passed target entities
Cake\Datasource\EntityInterface
$sourceEntity the entity from source table in this association
arrayCake\Datasource\EntityInterface>
$targetEntities list of entities to link to link to the source entity using the junction table
array<string, mixed>
$options list of options accepted by Table::save()
bool
_saveTarget(Cake\Datasource\EntityInterface $parentEntity, array $entities, array<string, mixed> $options): Cake\Datasource\EntityInterface|false
Persists each of the entities into the target table and creates links between the parent entity and each one of the saved target entities.
Cake\Datasource\EntityInterface
$parentEntity the source entity containing the target entities to be saved.
array
$entities list of entities to persist in target table and to link to the parent entity
array<string, mixed>
$options list of options accepted by Table::save()
Cake\Datasource\EntityInterface|false
InvalidArgumentException
_singularHumanName(string $name): string
Creates the singular human name used in views
string
$name Controller name
string
_singularName(string $name): string
Creates the singular name for use in views.
string
$name Name to use
string
_variableName(string $name): string
Creates the plural variable name for views
string
$name Name to use
string
attachTo(Cake\ORM\Query $query, array<string, mixed> $options = []): void
Alters a Query object to include the associated target table data in the final result
The options array accept the following keys:
Cake\ORM\Query
$query the query to be altered to include the target table data
array<string, mixed>
$options optional Any extra options or overrides to be taken in account
void
canBeJoined(array<string, mixed> $options = []): bool
Whether this association can be expressed directly in a query join
array<string, mixed>
$options optional custom options key that could alter the return value
bool
cascadeDelete(Cake\Datasource\EntityInterface $entity, array<string, mixed> $options = []): bool
Clear out the data in the junction table for a given entity.
Each implementing class should handle the cascaded delete as required.
Cake\Datasource\EntityInterface
$entity The entity that started the cascading delete.
array<string, mixed>
$options optional The options for the original delete.
bool
defaultRowValue(array<string, mixed> $row, bool $joined): array<string, mixed>
Returns a modified row after appending a property for this association with the default empty value according to whether the association was joined or fetched externally.
array<string, mixed>
$row bool
$joined array<string, mixed>
deleteAll(Cake\Database\ExpressionInterfaceClosure|array|string|null $conditions): int
Proxies the delete operation to the target table's deleteAll method
Cake\Database\ExpressionInterfaceClosure|array|string|null
$conditions Conditions to be used, accepts anything Query::where() can take.
int
eagerLoader(array<string, mixed> $options): Closure
Eager loads a list of records in the target table that are related to another set of records in the source table. Source records can be specified in two ways: first one is by passing a Query object setup to find on the source table and the other way is by explicitly passing an array of primary key values from the source table.
The required way of passing related source records is controlled by "strategy" When the subquery strategy is used it will require a query on the source table. When using the select strategy, the list of primary keys will be used.
Returns a closure that should be run for each record returned in a specific Query. This callable will be responsible for injecting the fields that are related to each specific passed row.
Options array accepts the following keys:
array<string, mixed>
$options Closure
exists(Cake\Database\ExpressionInterfaceClosure|array|string|null $conditions): bool
Proxies the operation to the target table's exists method after appending the default conditions for this association
Cake\Database\ExpressionInterfaceClosure|array|string|null
$conditions The conditions to use for checking if any record matches.
bool
fetchTable(string|null $alias = null, array<string, mixed> $options = []): Cake\ORM\Table
Convenience method to get a table instance.
string|null
$alias optional The alias name you want to get. Should be in CamelCase format. If null
then 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.
Cake\ORM\Table
Cake\Core\Exception\CakeException
find(array<string, mixed>|string|null $type = null, array<string, mixed> $options = []): Cake\ORM\Query
Proxies the finding operation to the target table's find method and modifies the query accordingly based of this association configuration.
If your association includes conditions or a finder, the junction table will be included in the query's contained associations.
array<string, mixed>|string|null
$type optional the type of query to perform, if an array is passed, it will be interpreted as the $options
parameter
array<string, mixed>
$options optional The options to for the find
Cake\ORM\Query
getBindingKey(): array<string>|string
Gets the name of the field representing the binding field with the target table. When not manually specified the primary key of the owning side table is used.
array<string>|string
getCascadeCallbacks(): bool
Gets whether cascaded deletes should also fire callbacks.
bool
getClassName(): string
Gets the class name of the target table object.
string
getConditions(): Closure|array
Gets a list of conditions to be always included when fetching records from the target association.
Closure|array
getDependent(): bool
Sets whether the records on the target table are dependent on the source table.
This is primarily used to indicate that records should be removed if the owning record in the source table is deleted.
bool
getFinder(): array|string
Gets the default finder to use for fetching rows from the target table.
array|string
getForeignKey(): array<string>|string
Gets the name of the field representing the foreign key to the source table.
array<string>|string
getJoinType(): string
Gets the type of join to be used when adding the association to a query.
string
getName(): string
Gets the name for this association, usually the alias assigned to the target associated table
string
getProperty(): string
Gets the property name that should be filled with data from the target table in the source table record.
string
getSaveStrategy(): string
Gets the strategy that should be used for saving.
string
getSort(): mixed
Gets the sort order in which target records should be returned.
mixed
getSource(): Cake\ORM\Table
Gets the table instance for the source side of the association.
Cake\ORM\Table
getStrategy(): string
Gets the strategy name to be used to fetch associated records. Keep in mind that some association types might not implement but a default strategy, rendering any changes to this setting void.
string
getTableLocator(): Cake\ORM\Locator\LocatorInterface
Gets the table locator.
Cake\ORM\Locator\LocatorInterface
getTarget(): Cake\ORM\Table
Gets the table instance for the target side of the association.
Cake\ORM\Table
getTargetForeignKey(): array<string>|string
Gets the name of the field representing the foreign key to the target table.
array<string>|string
getThrough(): Cake\ORM\Table|string
Gets the current join table, either the name of the Table instance or the instance itself.
Cake\ORM\Table|string
isOwningSide(Cake\ORM\Table $side): bool
Returns boolean true, as both of the tables 'own' rows in the other side of the association via the joint table.
Cake\ORM\Table
$side The potential Table with ownership
bool
junction(Cake\ORM\Table|string|null $table = null): Cake\ORM\Table
Sets the table instance for the junction relation. If no arguments are passed, the current configured table instance is returned
Cake\ORM\Table|string|null
$table optional Name or instance for the join table
Cake\ORM\Table
InvalidArgumentException
junctionConditions(): array
Returns filtered conditions that specifically reference the junction table.
array
link(Cake\Datasource\EntityInterface $sourceEntity, arrayCake\Datasource\EntityInterface> $targetEntities, array<string, mixed> $options = []): bool
Associates the source entity to each of the target entities provided by creating links in the junction table. Both the source entity and each of the target entities are assumed to be already persisted, if they are marked as new or their status is unknown then an exception will be thrown.
When using this method, all entities in $targetEntities
will be appended to the source entity's property corresponding to this association object.
This method does not check link uniqueness.
$newTags = $tags->find('relevant')->toArray(); $articles->getAssociation('tags')->link($article, $newTags);
$article->get('tags')
will contain all tags in $newTags
after liking
Cake\Datasource\EntityInterface
$sourceEntity the row belonging to the source
side of this association
arrayCake\Datasource\EntityInterface>
$targetEntities list of entities belonging to the target
side of this association
array<string, mixed>
$options optional list of options to be passed to the internal save
call
bool
InvalidArgumentException
replaceLinks(Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities, array<string, mixed> $options = []): bool
Replaces existing association links between the source entity and the target with the ones passed. This method does a smart cleanup, links that are already persisted and present in $targetEntities
will not be deleted, new links will be created for the passed target entities that are not already in the database and the rest will be removed.
For example, if an article is linked to tags 'cake' and 'framework' and you pass to this method an array containing the entities for tags 'cake', 'php' and 'awesome', only the link for cake will be kept in database, the link for 'framework' will be deleted and the links for 'php' and 'awesome' will be created.
Existing links are not deleted and created again, they are either left untouched or updated so that potential extra information stored in the joint row is not lost. Updating the link row can be done by making sure the corresponding passed target entity contains the joint property with its primary key and any extra information to be stored.
On success, the passed $sourceEntity
will contain $targetEntities
as value in the corresponding property for this association.
This method assumes that links between both the source entity and each of the target entities are unique. That is, for any given row in the source table there can only be one link in the junction table pointing to any other given row in the target table.
Additional options for new links to be saved can be passed in the third argument, check Table::save()
for information on the accepted options.
$article->tags = [$tag1, $tag2, $tag3, $tag4]; $articles->save($article); $tags = [$tag1, $tag3]; $articles->getAssociation('tags')->replaceLinks($article, $tags);
$article->get('tags')
will contain only [$tag1, $tag3]
at the end
Cake\Datasource\EntityInterface
$sourceEntity an entity persisted in the source table for this association
array
$targetEntities list of entities from the target table to be linked
array<string, mixed>
$options optional list of options to be passed to the internal save
/delete
calls when persisting/updating new links, or deleting existing ones
bool
InvalidArgumentException
requiresKeys(array<string, mixed> $options = []): bool
Returns true if the eager loading process will require a set of the owning table's binding keys in order to use them as a filter in the finder query.
array<string, mixed>
$options optional The options containing the strategy to be used.
bool
saveAssociated(Cake\Datasource\EntityInterface $entity, array<string, mixed> $options = []): Cake\Datasource\EntityInterface|false
Takes an entity from the source table and looks if there is a field matching the property name for this association. The found entity will be saved on the target table for this association by passing supplied $options
When using the 'append' strategy, this function will only create new links between each side of this association. It will not destroy existing ones even though they may not be present in the array of entities to be saved.
When using the 'replace' strategy, existing links will be removed and new links will be created in the joint table. If there exists links in the database to some of the entities intended to be saved by this method, they will be updated, not deleted.
Cake\Datasource\EntityInterface
$entity an entity from the source table
array<string, mixed>
$options optional options to be passed to the save method in the target table
Cake\Datasource\EntityInterface|false
InvalidArgumentException
setBindingKey(array<string>|string $key): $this
Sets the name of the field representing the binding field with the target table. When not manually specified the primary key of the owning side table is used.
array<string>|string
$key the table field or fields to be used to link both tables together
$this
setCascadeCallbacks(bool $cascadeCallbacks): $this
Sets whether cascaded deletes should also fire callbacks.
bool
$cascadeCallbacks cascade callbacks switch value
$this
setClassName(string $className): $this
Sets the class name of the target table object.
string
$className Class name to set.
$this
InvalidArgumentException
setConditions(Closure|array $conditions): $this
Sets a list of conditions to be always included when fetching records from the target association.
Closure|array
$conditions $this
setDependent(bool $dependent): $this
Sets whether the records on the target table are dependent on the source table.
This is primarily used to indicate that records should be removed if the owning record in the source table is deleted.
If no parameters are passed the current setting is returned.
bool
$dependent Set the dependent mode. Use null to read the current state.
$this
setFinder(array|string $finder): $this
Sets the default finder to use for fetching rows from the target table.
array|string
$finder the finder name to use or array of finder name and option.
$this
setForeignKey(array<string>|string $key): $this
Sets the name of the field representing the foreign key to the target table.
array<string>|string
$key the key or keys to be used to link both tables together
$this
setJoinType(string $type): $this
Sets the type of join to be used when adding the association to a query.
string
$type the join type to be used (e.g. INNER)
$this
setName(string $name): $this
Sets the name for this association, usually the alias assigned to the target associated table
string
$name Name to be assigned
$this
setProperty(string $name): $this
Sets the property name that should be filled with data from the target table in the source table record.
string
$name The name of the association property. Use null to read the current value.
$this
setSaveStrategy(string $strategy): $this
Sets the strategy that should be used for saving.
string
$strategy the strategy name to be used
$this
InvalidArgumentException
setSort(mixed $sort): $this
Sets the sort order in which target records should be returned.
mixed
$sort A find() compatible order clause
$this
setSource(Cake\ORM\Table $table): $this
Sets the table instance for the source side of the association.
Cake\ORM\Table
$table the instance to be assigned as source side
$this
setStrategy(string $name): $this
Sets the strategy name to be used to fetch associated records. Keep in mind that some association types might not implement but a default strategy, rendering any changes to this setting void.
string
$name The strategy type. Use null to read the current value.
$this
InvalidArgumentException
setTableLocator(Cake\ORM\Locator\LocatorInterface $tableLocator): $this
Sets the table locator.
Cake\ORM\Locator\LocatorInterface
$tableLocator LocatorInterface instance.
$this
setTarget(Cake\ORM\Table $table): $this
Sets the table instance for the target side of the association.
Cake\ORM\Table
$table the instance to be assigned as target side
$this
setTargetForeignKey(array<string>|string $key): $this
Sets the name of the field representing the foreign key to the target table.
array<string>|string
$key the key to be used to link both tables together
$this
setThrough(Cake\ORM\Table|string $through): $this
Sets the current join table, either the name of the Table instance or the instance itself.
Cake\ORM\Table|string
$through Name of the Table instance or the instance itself
$this
targetConditions(): arrayClosure|null
Returns filtered conditions that reference the target table.
Any string expressions, or expression objects will also be returned in this list.
arrayClosure|null
transformRow(array $row, string $nestKey, bool $joined, string|null $targetProperty = null): array
Correctly nests a result row associated values into the correct array keys inside the source results.
array
$row The row to transform
string
$nestKey The array key under which the results for this association should be found
bool
$joined Whether the row is a result of a direct join with this association
string|null
$targetProperty optional The property name in the source results where the association data shuld be nested in. Will use the default one if not provided.
array
type(): string
Get the relationship type.
string
unlink(Cake\Datasource\EntityInterface $sourceEntity, arrayCake\Datasource\EntityInterface> $targetEntities, array<string>|bool $options = []): bool
Removes all links between the passed source entity and each of the provided target entities. This method assumes that all passed objects are already persisted in the database and that each of them contain a primary key value.
Additionally to the default options accepted by Table::delete()
, the following keys are supported:
$targetEntities
that are stored in $sourceEntity
(default: true)By default this method will unset each of the entity objects stored inside the source entity.
$article->tags = [$tag1, $tag2, $tag3, $tag4]; $tags = [$tag1, $tag2, $tag3]; $articles->getAssociation('tags')->unlink($article, $tags);
$article->get('tags')
will contain only [$tag4]
after deleting in the database
Cake\Datasource\EntityInterface
$sourceEntity An entity persisted in the source table for this association.
arrayCake\Datasource\EntityInterface>
$targetEntities List of entities persisted in the target table for this association.
array<string>|bool
$options optional List of options to be passed to the internal delete
call, or a boolean
as cleanProperty
key shortcut.
bool
InvalidArgumentException
updateAll(array $fields, Cake\Database\ExpressionInterfaceClosure|array|string|null $conditions): int
Proxies the update operation to the target table's updateAll method
array
$fields A hash of field => new value.
Cake\Database\ExpressionInterfaceClosure|array|string|null
$conditions Conditions to be used, accepts anything Query::where() can take.
int
The field name in the owning side table that is used to match with the foreignKey
array<string>|string|null
Whether cascaded deletes should also fire callbacks.
bool
The class name of the target table object
string
A list of conditions to be always included when fetching records from the target association
Closure|array
Whether the records on the joint table should be removed when a record on the source table is deleted.
Defaults to true for backwards compatibility.
bool
The default finder name to use for fetching rows from the target table With array value, finder name and default options are allowed.
array|string
The name of the field representing the foreign key to the table to load
array<string>|string
The type of join to be used when adding the association to a query
string
The name of the hasMany association from the target table to the junction table
string
Filtered conditions that reference the junction table.
array|null
The name of the property to be set containing data from the junction table once a record from the target table is hydrated
string
Junction table instance
Cake\ORM\Table
Junction table name
string
Name given to the association, it usually represents the alias assigned to the target associated table
string
The property name that should be filled with data from the target table in the source table record.
string
Saving strategy to be used by this association
string
Order in which target records should be returned
mixed
Source table instance
Cake\ORM\Table
The strategy name to be used to fetch associated records.
string
Table locator instance
Cake\ORM\Locator\LocatorInterface|null
Filtered conditions that reference the target table.
array|null
The name of the field representing the foreign key to the target table
array<string>|string|null
Target table instance
Cake\ORM\Table
The table instance for the junction relation.
Cake\ORM\Table|string
Valid strategies for this type of association
array<string>
This object's default table alias.
string|null
© 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.4/class-Cake.ORM.Association.BelongsToMany.html