An Association is a relationship established between two tables and is used to configure and customize the way interconnected records are retrieved.
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 '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|nullThe field name in the owning side table that is used to match with the foreignKey
boolWhether cascaded deletes should also fire callbacks.
stringThe class name of the target table object
Closure|arrayA list of conditions to be always included when fetching records from the target association
boolWhether the records on the target table are dependent on the source table, often used to indicate that records should be removed if the owning record in the source table is deleted.
array|stringThe default finder name to use for fetching rows from the target table With array value, finder name and default options are allowed.
array<string>|stringThe name of the field representing the foreign key to the table to load
stringThe type of join to be used when adding the association to a query
stringName given to the association, it usually represents the alias assigned to the target associated table
stringThe property name that should be filled with data from the target table in the source table record.
Cake\ORM\TableSource table instance
stringThe strategy name to be used to fetch associated records. Some association types might not implement but one strategy to fetch records.
Cake\ORM\Locator\LocatorInterface|nullTable locator instance
Cake\ORM\TableTarget table instance
array<string>Valid strategies for this association. Subclasses can narrow this down.
string|nullThis 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.
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
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.
Returns a single or multiple conditions to be appended to the generated join clause for getting the results on the target table.
Creates the proper underscored model key for associations
Creates the proper model name from a foreign key
Override this function to initialize any concrete association class, it will get passed the original list of options used 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 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
Handles cascading a delete from an associated model.
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 target 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 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.
Returns whether the passed table is the owning side for this association. This means that rows in the 'target' table would miss important or required information if the row in 'source' did not exist.
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.
Extract the target's association data our from the passed entity and proxies the saving operation to the target table.
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 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.
Correctly nests a result row associated values into the correct array keys inside the source results.
Get the relationship type.
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
mixedBadMethodCallException__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\AssociationRuntimeException__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_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 The query to modify
array<string, mixed> $options Options array containing the negateMatch key.
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_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_joinCondition(array<string, mixed> $options): array
Returns a single or multiple conditions to be appended to the generated join clause for getting the results on the target table.
array<string, mixed> $options list of options passed to attachTo method
arrayRuntimeException_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
Override this function to initialize any concrete association class, it will get passed the original list of options used in the constructor
array<string, mixed> $options List of options used for initialization
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_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
stringattachTo(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
voidRuntimeExceptioncanBeJoined(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
boolcascadeDelete(Cake\Datasource\EntityInterface $entity, array<string, mixed> $options = []): bool
Handles cascading a delete from an associated model.
Each implementing class should handle the cascaded delete as required.
Cake\Datasource\EntityInterface $entity The entity that started the cascaded delete.
array<string, mixed> $options optional The options for the original delete.
booldefaultRowValue(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 The row to set a default on.
bool $joined Whether the row is a result of a direct join with this association
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.
inteagerLoader(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 The options for eager loading.
Closureexists(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.
boolfetchTable(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\TableCake\Core\Exception\CakeExceptionfind(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
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\QuerygetBindingKey(): 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>|stringgetCascadeCallbacks(): bool
Gets whether cascaded deletes should also fire callbacks.
boolgetClassName(): string
Gets the class name of the target table object.
stringgetConditions(): Closure|array
Gets a list of conditions to be always included when fetching records from the target association.
Closure|arraygetDependent(): 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.
boolgetFinder(): array|string
Gets the default finder to use for fetching rows from the target table.
array|stringgetForeignKey(): array<string>|string
Gets the name of the field representing the foreign key to the target table.
array<string>|stringgetJoinType(): string
Gets the type of join to be used when adding the association to a query.
stringgetName(): string
Gets the name for this association, usually the alias assigned to the target associated table
stringgetProperty(): string
Gets the property name that should be filled with data from the target table in the source table record.
stringgetSource(): Cake\ORM\Table
Gets the table instance for the source side of the association.
Cake\ORM\TablegetStrategy(): 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.
stringgetTableLocator(): Cake\ORM\Locator\LocatorInterface
Gets the table locator.
Cake\ORM\Locator\LocatorInterfacegetTarget(): Cake\ORM\Table
Gets the table instance for the target side of the association.
Cake\ORM\TableisOwningSide(Cake\ORM\Table $side): bool
Returns whether the passed table is the owning side for this association. This means that rows in the 'target' table would miss important or required information if the row in 'source' did not exist.
Cake\ORM\Table $side The potential Table with ownership
boolrequiresKeys(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.
boolsaveAssociated(Cake\Datasource\EntityInterface $entity, array<string, mixed> $options = []): Cake\Datasource\EntityInterface|false
Extract the target's association data our from the passed entity and proxies the saving operation to the target table.
Cake\Datasource\EntityInterface $entity the data to be saved
array<string, mixed> $options optional The options for saving associated data.
Cake\Datasource\EntityInterface|falsesetBindingKey(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
$thissetCascadeCallbacks(bool $cascadeCallbacks): $this
Sets whether cascaded deletes should also fire callbacks.
bool $cascadeCallbacks cascade callbacks switch value
$thissetClassName(string $className): $this
Sets the class name of the target table object.
string $className Class name to set.
$thisInvalidArgumentExceptionsetConditions(Closure|array $conditions): $this
Sets a list of conditions to be always included when fetching records from the target association.
Closure|array $conditions list of conditions to be used
$thissetDependent(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.
$thissetFinder(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.
$thissetForeignKey(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
$thissetJoinType(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)
$thissetName(string $name): $this
Sets the name for this association, usually the alias assigned to the target associated table
string $name Name to be assigned
$thissetProperty(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.
$thissetSource(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
$thissetStrategy(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.
$thisInvalidArgumentExceptionsetTableLocator(Cake\ORM\Locator\LocatorInterface $tableLocator): $this
Sets the table locator.
Cake\ORM\Locator\LocatorInterface $tableLocator LocatorInterface instance.
$thissetTarget(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
$thistransformRow(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.
arraytype(): string
Get the relationship type.
stringupdateAll(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.
intThe field name in the owning side table that is used to match with the foreignKey
array<string>|string|nullWhether cascaded deletes should also fire callbacks.
boolThe class name of the target table object
stringA list of conditions to be always included when fetching records from the target association
Closure|arrayWhether the records on the target table are dependent on the source table, often used to indicate that records should be removed if the owning record in the source table is deleted.
boolThe default finder name to use for fetching rows from the target table With array value, finder name and default options are allowed.
array|stringThe name of the field representing the foreign key to the table to load
array<string>|stringThe type of join to be used when adding the association to a query
stringName given to the association, it usually represents the alias assigned to the target associated table
stringThe property name that should be filled with data from the target table in the source table record.
stringSource table instance
Cake\ORM\TableThe strategy name to be used to fetch associated records. Some association types might not implement but one strategy to fetch records.
stringTable locator instance
Cake\ORM\Locator\LocatorInterface|nullTarget table instance
Cake\ORM\TableValid strategies for this association. Subclasses can narrow this down.
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.html