Class HasMany
Represents an N - 1 relationship where the target side of the relationship will have one or multiple records per each one in the source side.
An example of a HasMany association would be Author has many Articles.
Constants
- 
          
          stringMANY_TO_MANY ¶'manyToMany'Association type for many to many associations. 
- 
          
          stringMANY_TO_ONE ¶'manyToOne'Association type for many to one associations. 
- 
          
          stringONE_TO_MANY ¶'oneToMany'Association type for one to many associations. 
- 
          
          stringONE_TO_ONE ¶'oneToOne'Association type for one to one associations. 
- 
          
          stringSAVE_APPEND ¶'append'Saving strategy that will only append to the links set 
- 
          
          stringSAVE_REPLACE ¶'replace'Saving strategy that will replace the links with the provided set 
- 
          
          stringSTRATEGY_JOIN ¶'join'Strategy name to use joins for fetching associated records 
- 
          
          stringSTRATEGY_SELECT ¶'select'Strategy name to use a select for fetching associated records 
- 
          
          stringSTRATEGY_SUBQUERY ¶'subquery'Strategy name to use a subquery for fetching associated records 
Property Summary
- 
        $_bindingKey protectedstring|arrayThe field name in the owning side table that is used to match with the foreignKey 
- 
        $_cascadeCallbacks protectedboolWhether or not cascaded deletes should also fire callbacks. 
- 
        $_className protectedstringThe class name of the target table object 
- 
        $_conditions protectedarrayA list of conditions to be always included when fetching records from the target association 
- 
        $_dependent protectedboolWhether 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. 
- 
        $_finder protectedstringThe default finder name to use for fetching rows from the target table 
- 
        $_foreignKey protectedstring|arrayThe name of the field representing the foreign key to the table to load 
- 
        $_joinType protectedstringThe type of join to be used when adding the association to a query 
- 
        $_name protectedstringName given to the association, it usually represents the alias assigned to the target associated table 
- 
        $_propertyName protectedstringThe property name that should be filled with data from the target table in the source table record. 
- 
        $_saveStrategy protectedstringSaving strategy to be used by this association 
- 
        $_sort protectedmixedOrder in which target records should be returned 
- 
        $_sourceTable protectedCake\ORM\TableSource table instance 
- 
        $_strategy protectedstringThe strategy name to be used to fetch associated records. 
- 
        $_tableLocator protectedCake\ORM\Locator\LocatorInterfaceTable locator instance 
- 
        $_targetTable protectedCake\ORM\TableTarget table instance 
- 
        $_validStrategies protectedarrayValid strategies for this type of association 
Method Summary
- 
          __call() publicProxies method calls to the target table. 
- 
          __construct() publicConstructor. Subclasses can override _options function to get the original list of passed options if expecting any other special key 
- 
          __get() publicProxies property retrieval to the target table. This is handy for getting this association's associations 
- 
          __isset() publicProxies the isset call to the target table. This is handy to check if the target table has another association with the passed name 
- 
          _addFilteringCondition() protectedAppends any conditions required to load the relevant set of records in the target table query given a filter key and some filtering values. 
- 
          _addFilteringJoin() publicAppends any conditions required to load the relevant set of records in the target table query given a filter key and some filtering values when the filtering needs to be done using a subquery. 
- 
          _appendFields() protectedHelper function used to conditionally append fields to the select clause of a query from the fields found in another query object. 
- 
          _appendNotMatching() protectedConditionally adds a condition to the passed Query that will make it find records where there is no match with this association. 
- 
          _assertFieldsPresent() protectedChecks that the fetching query either has auto fields on or has the foreignKey fields selected. If the required fields are missing, throws an exception. 
- 
          _bindNewAssociations() protectedApplies all attachable associations to $queryout of the containments found in the$surrogatequery.
- 
          _buildQuery() protectedAuxiliary function to construct a new Query object to return all the records in the target table that are associated to those specified in $options from the source table 
- 
          _buildResultMap() protectedBuilds an array containing the results from fetchQuery indexed by the foreignKey value corresponding to this association. 
- 
          _buildSubquery() protectedBuilds a query to be used as a condition for filtering records in the target table, it is constructed by cloning the original query that was used to load records in the source table. 
- 
          _camelize() protectedCreates a camelized version of $name 
- 
          _createTupleCondition() protectedReturns a TupleComparison object that can be used for matching all the fields from $keys with the tuple values in $filter using the provided operator. 
- 
          _defaultOptions() protectedReturns the default options to use for the eagerLoader 
- 
          _dispatchBeforeFind() protectedTriggers beforeFind on the target table for the query this association is attaching to 
- 
          _entityName() protectedCreates the proper entity name (singular) for the specified name 
- 
          _extractFinder() protectedHelper method to infer the requested finder and its options. 
- 
          _fixtureName() protectedCreates a fixture name 
- 
          _foreignKeyAcceptsNull() protectedChecks the nullable flag of the foreign key 
- 
          _formatAssociationResults() protectedAdds a formatter function to the passed $queryif the$surrogatequery declares any other formatter. Since the$surrogatequery 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.
- 
          _joinCondition() protectedReturns a single or multiple conditions to be appended to the generated join clause for getting the results on the target table. 
- 
          _linkField() protectedGenerates a string used as a table field that contains the values upon which the filter should be applied 
- 
          _modelKey() protectedCreates the proper underscored model key for associations 
- 
          _modelNameFromKey() protectedCreates the proper model name from a foreign key 
- 
          _multiKeysInjector() protectedReturns a callable to be used for each row in a query result set for injecting the eager loaded rows when the matching needs to be done with multiple foreign keys 
- 
          _options() protectedParse extra options passed in the constructor. 
- 
          _pluginNamespace() protectedReturn plugin's namespace 
- 
          _pluginPath() protectedFind the correct path for a plugin. Scans $pluginPaths for the plugin you want. 
- 
          _pluralHumanName() protectedCreates the plural human name used in views 
- 
          _propertyName() protectedReturns default property name based on association name. 
- 
          _resultInjector() protectedReturns a callable to be used for each row in a query result set for injecting the eager loaded rows 
- 
          _singularHumanName() protectedCreates the singular human name used in views 
- 
          _singularName() protectedCreates the singular name for use in views. 
- 
          _subqueryFields() protectedCalculate the fields that need to participate in a subquery. 
- 
          _unlink() protectedDeletes/sets null the related objects matching $conditions. The action which is taken depends on the dependency between source and targets and also on foreign key nullability 
- 
          _unlinkAssociated() protectedDeletes/sets null the related objects according to the dependency between source and targets and foreign key nullability Skips deleting records present in $remainingEntities 
- 
          _variableName() protectedCreates the plural variable name for views 
- 
          attachTo() publicAlters a Query object to include the associated target table data in the final result 
- 
          bindingKey() publicSets 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. 
- 
          canBeJoined() publicWhether this association can be expressed directly in a query join 
- 
          cascadeCallbacks() publicSets whether or not cascaded deletes should also fire callbacks. If no arguments are passed, the current configured value is returned 
- 
          cascadeDelete() publicCascade a delete to remove dependent records. 
- 
          className() publicThe class name of the target table object 
- 
          conditions() publicSets a list of conditions to be always included when fetching records from the target association. If no parameters are passed the current list is returned 
- 
          defaultRowValue() publicReturns 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. 
- 
          deleteAll() publicProxies the delete operation to the target table's deleteAll method 
- 
          dependent() publicSets whether the records on the target table are dependent on the source table. 
- 
          eagerLoader() publicEager loads a list of records in the target table that are related to another set of records in the source table. Source records can 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. 
- 
          exists() publicProxies the operation to the target table's exists method after appending the default conditions for this association 
- 
          find() publicProxies the finding operation to the target table's find method and modifies the query accordingly based of this association configuration 
- 
          finder() publicSets the default finder to use for fetching rows from the target table. If no parameters are passed, it will return the currently configured finder name. 
- 
          foreignKey() publicSets the name of the field representing the foreign key to the source table. If no parameters are passed current field is returned 
- 
          isOwningSide() publicReturns whether or not 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. 
- 
          joinType() publicSets the type of join to be used when adding the association to a query. If no arguments are passed, the currently configured type is returned. 
- 
          link() publicAssociates the source entity to each of the target entities provided. When using this method, all entities in $targetEntitieswill be appended to the source entity's property corresponding to this association object.
- 
          name() publicSets the name for this association. If no argument is passed then the current configured name will be returned 
- 
          property() publicSets the property name that should be filled with data from the target table in the source table record. If no arguments are passed, the currently configured type is returned. 
- 
          replace() publicReplaces 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 $targetEntitieswill 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.
- 
          requiresKeys() publicReturns 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. 
- 
          saveAssociated() publicTakes 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
- 
          saveStrategy() publicSets the strategy that should be used for saving. If called with no arguments, it will return the currently configured strategy 
- 
          sort() publicSets the sort order in which target records should be returned. If no arguments are passed the currently configured value is returned 
- 
          source() publicSets the table instance for the source side of the association. If no arguments are passed, the current configured table instance is returned 
- 
          strategy() publicSets 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. If no arguments are passed, the currently configured strategy is returned. 
- 
          tableLocator() publicSets the table locator. If no parameters are passed, it will return the currently used locator. 
- 
          target() publicSets the table instance for the target side of the association. If no arguments are passed, the current configured table instance is returned 
- 
          transformRow() publicCorrectly nests a result row associated values into the correct array keys inside the source results. 
- 
          type() publicGet the relationship type. 
- 
          unlink() publicRemoves 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. 
- 
          updateAll() publicProxies the update operation to the target table's updateAll method 
Method Detail
__call() ¶ public
__call(string $method, array $argument): mixedProxies method calls to the target table.
Parameters
- 
                string$method
- name of the method to be invoked 
- 
                array$argument
- List of arguments passed to the function 
Returns
mixedThrows
BadMethodCallException__construct() ¶ public
__construct(string $alias, array $options = [])Constructor. Subclasses can override _options function to get the original list of passed options if expecting any other special key
Parameters
- 
                string$alias
- The name given to the association 
- 
                array$options optional
- A list of properties to be set on this object 
__get() ¶ public
__get(string $property): Cake\ORM\AssociationProxies property retrieval to the target table. This is handy for getting this association's associations
Parameters
- 
                string$property
- the property name 
Returns
Cake\ORM\AssociationThrows
RuntimeExceptionif no association with such name exists
__isset() ¶ public
__isset(string $property): boolProxies the isset call to the target table. This is handy to check if the target table has another association with the passed name
Parameters
- 
                string$property
- the property name 
Returns
booltrue if the property exists
_addFilteringCondition() ¶ protected
_addFilteringCondition(Cake\ORM\Query $query, string|array $key, mixed $filter): Cake\ORM\QueryAppends any conditions required to load the relevant set of records in the target table query given a filter key and some filtering values.
Parameters
- 
                Cake\ORM\Query$query
- Target table's query 
- 
                string|array$key
- the fields that should be used for filtering 
- 
                mixed$filter
- the value that should be used to match for $key 
Returns
Cake\ORM\Query_addFilteringJoin() ¶ public
_addFilteringJoin(Cake\ORM\Query $query, string $key, Cake\ORM\Query $subquery): Cake\ORM\QueryAppends any conditions required to load the relevant set of records in the target table query given a filter key and some filtering values when the filtering needs to be done using a subquery.
Parameters
- 
                Cake\ORM\Query$query
- Target table's query 
- 
                string$key
- the fields that should be used for filtering 
- 
                Cake\ORM\Query$subquery
- The Subquery to use for filtering 
Returns
Cake\ORM\Query_appendFields() ¶ protected
_appendFields(Cake\ORM\Query $query, Cake\ORM\Query $surrogate, array $options): voidHelper function used to conditionally append fields to the select clause of a query from the fields found in another query object.
Parameters
- 
                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$options
- options passed to the method - attachTo
Returns
void_appendNotMatching() ¶ protected
_appendNotMatching(Cake\Datasource\QueryInterface $query, array $options): voidConditionally adds a condition to the passed Query that will make it find records where there is no match with this association.
Parameters
- 
                Cake\Datasource\QueryInterface$query
- The query to modify 
- 
                array$options
- Options array containing the - negateMatchkey.
Returns
void_assertFieldsPresent() ¶ protected
_assertFieldsPresent(Cake\ORM\Query $fetchQuery, array $key): voidChecks that the fetching query either has auto fields on or has the foreignKey fields selected. If the required fields are missing, throws an exception.
Parameters
- 
                Cake\ORM\Query$fetchQuery
- The association fetching query 
- 
                array$key
- The foreign key fields to check 
Returns
voidThrows
InvalidArgumentException_bindNewAssociations() ¶ protected
_bindNewAssociations(Cake\ORM\Query $query, Cake\ORM\Query $surrogate, array $options): voidApplies 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.
Parameters
- 
                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$options
- options passed to the method - attachTo
Returns
void_buildQuery() ¶ protected
_buildQuery(array $options): Cake\ORM\QueryAuxiliary function to construct a new Query object to return all the records in the target table that are associated to those specified in $options from the source table
Parameters
- 
                array$options
- options accepted by eagerLoader() 
Returns
Cake\ORM\QueryThrows
InvalidArgumentExceptionWhen a key is required for associations but not selected.
_buildResultMap() ¶ protected
_buildResultMap(Cake\ORM\Query $fetchQuery, array $options): arrayBuilds an array containing the results from fetchQuery indexed by the foreignKey value corresponding to this association.
Parameters
- 
                Cake\ORM\Query$fetchQuery
- 
                array$options
Returns
array_buildSubquery() ¶ protected
_buildSubquery(Cake\ORM\Query $query): Cake\ORM\QueryBuilds a query to be used as a condition for filtering records in the target table, it is constructed by cloning the original query that was used to load records in the source table.
Parameters
- 
                Cake\ORM\Query$query
- the original query used to load source records 
Returns
Cake\ORM\Query_camelize() ¶ protected
_camelize(string $name): stringCreates a camelized version of $name
Parameters
- 
                string$name
- name 
Returns
stringCamelized name
_createTupleCondition() ¶ protected
_createTupleCondition(Cake\ORM\Query $query, array $keys, mixed $filter, string $operator): Cake\Database\Expression\TupleComparisonReturns a TupleComparison object that can be used for matching all the fields from $keys with the tuple values in $filter using the provided operator.
Parameters
- 
                Cake\ORM\Query$query
- Target table's query 
- 
                array$keys
- the fields that should be used for filtering 
- 
                mixed$filter
- the value that should be used to match for $key 
- 
                string$operator
- The operator for comparing the tuples 
Returns
Cake\Database\Expression\TupleComparison_defaultOptions() ¶ protected
_defaultOptions(): arrayReturns the default options to use for the eagerLoader
Returns
array_dispatchBeforeFind() ¶ protected
_dispatchBeforeFind(Cake\ORM\Query $query): voidTriggers beforeFind on the target table for the query this association is attaching to
Parameters
- 
                Cake\ORM\Query$query
- the query this association is attaching itself to 
Returns
void_entityName() ¶ protected
_entityName(string $name): stringCreates the proper entity name (singular) for the specified name
Parameters
- 
                string$name
- Name 
Returns
stringCamelized and plural model name
_extractFinder() ¶ protected
_extractFinder(string|array $finderData): arrayHelper method to infer the requested finder and its options.
Returns the inferred options from the finder $type.
Examples:
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']]]]]);
Parameters
- 
                string|array$finderData
- The finder name or an array having the name as key and options as value. 
Returns
array_fixtureName() ¶ protected
_fixtureName(string $name): stringCreates a fixture name
Parameters
- 
                string$name
- Model class name 
Returns
stringSingular model key
_foreignKeyAcceptsNull() ¶ protected
_foreignKeyAcceptsNull(Cake\ORM\Table $table, array $properties): boolChecks the nullable flag of the foreign key
Parameters
- 
                Cake\ORM\Table$table
- the table containing the foreign key 
- 
                array$properties
- the list of fields that compose the foreign key 
Returns
bool_formatAssociationResults() ¶ protected
_formatAssociationResults(Cake\ORM\Query $query, Cake\ORM\Query $surrogate, array $options): voidAdds 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.
Parameters
- 
                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$options
- options passed to the method - attachTo
Returns
void_joinCondition() ¶ protected
_joinCondition(array $options): arrayReturns a single or multiple conditions to be appended to the generated join clause for getting the results on the target table.
Parameters
- 
                array$options
- list of options passed to attachTo method 
Returns
arrayThrows
RuntimeExceptionif the number of columns in the foreignKey do not match the number of columns in the source table primaryKey
_linkField() ¶ protected
_linkField(array $options): string|arrayGenerates a string used as a table field that contains the values upon which the filter should be applied
Parameters
- 
                array$options
Returns
string|array_modelKey() ¶ protected
_modelKey(string $name): stringCreates the proper underscored model key for associations
If the input contains a dot, assume that the right side is the real table name.
Parameters
- 
                string$name
- Model class name 
Returns
stringSingular model key
_modelNameFromKey() ¶ protected
_modelNameFromKey(string $key): stringCreates the proper model name from a foreign key
Parameters
- 
                string$key
- Foreign key 
Returns
stringModel name
_multiKeysInjector() ¶ protected
_multiKeysInjector(array $resultMap, array $sourceKeys, string $nestKey): ClosureReturns a callable to be used for each row in a query result set for injecting the eager loaded rows when the matching needs to be done with multiple foreign keys
Parameters
- 
                array$resultMap
- A keyed arrays containing the target table 
- 
                array$sourceKeys
- An array with aliased keys to match 
- 
                string$nestKey
- The key under which results should be nested 
Returns
Closure_options() ¶ protected
_options(array $opts): voidParse extra options passed in the constructor.
Parameters
- 
                array$opts
- original list of options passed in constructor 
Returns
void_pluginNamespace() ¶ protected
_pluginNamespace(string $pluginName): stringReturn plugin's namespace
Parameters
- 
                string$pluginName
- Plugin name 
Returns
stringPlugin's namespace
_pluginPath() ¶ protected
_pluginPath(string $pluginName): stringFind the correct path for a plugin. Scans $pluginPaths for the plugin you want.
Parameters
- 
                string$pluginName
- Name of the plugin you want ie. DebugKit 
Returns
stringpath path to the correct plugin.
_pluralHumanName() ¶ protected
_pluralHumanName(string $name): stringCreates the plural human name used in views
Parameters
- 
                string$name
- Controller name 
Returns
stringPlural human name
_propertyName() ¶ protected
_propertyName(): stringReturns default property name based on association name.
Returns
string_resultInjector() ¶ protected
_resultInjector(Cake\ORM\Query $fetchQuery, array $resultMap, array $options): ClosureReturns a callable to be used for each row in a query result set for injecting the eager loaded rows
Parameters
- 
                Cake\ORM\Query$fetchQuery
- the Query used to fetch results 
- 
                array$resultMap
- an array with the foreignKey as keys and the corresponding target table results as value. 
- 
                array$options
- The options passed to the eagerLoader method 
Returns
Closure_singularHumanName() ¶ protected
_singularHumanName(string $name): stringCreates the singular human name used in views
Parameters
- 
                string$name
- Controller name 
Returns
stringSingular human name
_singularName() ¶ protected
_singularName(string $name): stringCreates the singular name for use in views.
Parameters
- 
                string$name
- Name to use 
Returns
stringVariable name
_subqueryFields() ¶ protected
_subqueryFields(Cake\ORM\Query $query): arrayCalculate the fields that need to participate in a subquery.
Normally this includes the binding key columns. If there is a an ORDER BY, those columns are also included as the fields may be calculated or constant values, that need to be present to ensure the correct association data is loaded.
Parameters
- 
                Cake\ORM\Query$query
- The query to get fields from. 
Returns
arrayThe list of fields for the subquery.
_unlink() ¶ protected
_unlink(array $foreignKey, Cake\ORM\Table $target, array $conditions = [], array $options = []): boolDeletes/sets null the related objects matching $conditions. The action which is taken depends on the dependency between source and targets and also on foreign key nullability
Parameters
- 
                array$foreignKey
- array of foreign key properties 
- 
                Cake\ORM\Table$target
- The associated table 
- 
                array$conditions optional
- The conditions that specifies what are the objects to be unlinked 
- 
                array$options optional
- list of options accepted by - Table::delete()
Returns
boolsuccess
_unlinkAssociated() ¶ protected
_unlinkAssociated(array $properties, Cake\Datasource\EntityInterface $entity, Cake\ORM\Table $target, array $remainingEntities = [], array $options = []): boolDeletes/sets null the related objects according to the dependency between source and targets and foreign key nullability Skips deleting records present in $remainingEntities
Parameters
- 
                array$properties
- array of foreignKey properties 
- 
                Cake\Datasource\EntityInterface$entity
- the entity which should have its associated entities unassigned 
- 
                Cake\ORM\Table$target
- The associated table 
- 
                array$remainingEntities optional
- Entities that should not be deleted 
- 
                array$options optional
- list of options accepted by - Table::delete()
Returns
boolsuccess
_variableName() ¶ protected
_variableName(string $name): stringCreates the plural variable name for views
Parameters
- 
                string$name
- Name to use 
Returns
stringPlural name for views
attachTo() ¶ public
attachTo(Cake\ORM\Query $query, array $options = []): voidAlters a Query object to include the associated target table data in the final result
The options array accept the following keys:
- includeFields: Whether to include target model fields in the result or not
- foreignKey: The name of the field to use as foreign key, if false none will be used
- conditions: array with a list of conditions to filter the join with, this will be merged with any conditions originally configured for this association
- fields: a list of fields in the target table to include in the result
- type: The type of join to be used (e.g. INNER) the records found on this association
- aliasPath: A dot separated string representing the path of association names followed from the passed query main table to this association.
- propertyPath: A dot separated string representing the path of association properties to be followed from the passed query main entity to this association
- joinType: The SQL join type to use in the query.
- negateMatch: Will append a condition to the passed query for excluding matches. with this association.
Parameters
- 
                Cake\ORM\Query$query
- the query to be altered to include the target table data 
- 
                array$options optional
- Any extra options or overrides to be taken in account 
Returns
voidThrows
RuntimeExceptionif the query builder passed does not return a query object
bindingKey() ¶ public
bindingKey(string|null $key = null): string|arraySets 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.
If no parameters are passed the current field is returned
Parameters
- 
                string|null$key optional
- the table field to be used to link both tables together 
Returns
string|arraycanBeJoined() ¶ public
canBeJoined(array $options = []): boolWhether this association can be expressed directly in a query join
Parameters
- 
                array$options optional
- custom options key that could alter the return value 
Returns
boolif the 'matching' key in $option is true then this function will return true, false otherwise
cascadeCallbacks() ¶ public
cascadeCallbacks(bool|null $cascadeCallbacks = null): boolSets whether or not cascaded deletes should also fire callbacks. If no arguments are passed, the current configured value is returned
Parameters
- 
                bool|null$cascadeCallbacks optional
- cascade callbacks switch value 
Returns
boolcascadeDelete() ¶ public
cascadeDelete(Cake\Datasource\EntityInterface $entity, array $options = []): boolCascade a delete to remove dependent records.
This method does nothing if the association is not dependent.
Parameters
- 
                Cake\Datasource\EntityInterface$entity
- The entity that started the cascaded delete. 
- 
                array$options optional
- The options for the original delete. 
Returns
boolSuccess.
conditions() ¶ public
conditions(array|null $conditions = null): arraySets a list of conditions to be always included when fetching records from the target association. If no parameters are passed the current list is returned
Parameters
- 
                array|null$conditions optional
- list of conditions to be used 
Returns
arraySee Also
defaultRowValue() ¶ public
defaultRowValue(array $row, bool $joined): arrayReturns 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.
Parameters
- 
                array$row
- 
                bool$joined
Returns
arraydeleteAll() ¶ public
deleteAll(mixed $conditions): intProxies the delete operation to the target table's deleteAll method
Parameters
- 
                mixed$conditions
- Conditions to be used, accepts anything Query::where() can take. 
Returns
intReturns the number of affected rows.
See Also
dependent() ¶ public
dependent(bool|null $dependent = null): boolSets 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.
Parameters
- 
                bool|null$dependent optional
- Set the dependent mode. Use null to read the current state. 
Returns
booleagerLoader() ¶ public
eagerLoader(array $options): ClosureEager loads a list of records in the target table that are related to another set of records in the source table. Source records can 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:
- query: Query object setup to find the source table records
- keys: List of primary key values from the source table
- foreignKey: The name of the field used to relate both tables
- conditions: List of conditions to be passed to the query where() method
- sort: The direction in which the records should be returned
- fields: List of fields to select from the target table
- contain: List of related tables to eager load associated to the target table
- strategy: The name of strategy to use for finding target table records
- nestKey: The array key under which results will be found when transforming the row
Parameters
- 
                array$options
Returns
Closureexists() ¶ public
exists(array|callable|Cake\Database\ExpressionInterface $conditions): boolProxies the operation to the target table's exists method after appending the default conditions for this association
Parameters
- 
                array|callable|Cake\Database\ExpressionInterface$conditions
- The conditions to use for checking if any record matches. 
Returns
boolSee Also
find() ¶ public
find(string|array|null $type = null, array $options = []): Cake\ORM\QueryProxies the finding operation to the target table's find method and modifies the query accordingly based of this association configuration
Parameters
- 
                string|array|null$type optional
- the type of query to perform, if an array is passed, it will be interpreted as the - $optionsparameter
- 
                array$options optional
- The options to for the find 
Returns
Cake\ORM\QuerySee Also
finder() ¶ public
finder(string|null $finder = null): stringSets the default finder to use for fetching rows from the target table. If no parameters are passed, it will return the currently configured finder name.
Parameters
- 
                string|null$finder optional
- the finder name to use 
Returns
stringforeignKey() ¶ public
foreignKey(string|null $key = null): stringSets the name of the field representing the foreign key to the source table. If no parameters are passed current field is returned
Parameters
- 
                string|null$key optional
- the key to be used to link both tables together 
Returns
stringisOwningSide() ¶ public
isOwningSide(Cake\ORM\Table $side): boolReturns whether or not 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.
Parameters
- 
                Cake\ORM\Table$side
- The potential Table with ownership 
Returns
booljoinType() ¶ public
joinType(string|null $type = null): stringSets the type of join to be used when adding the association to a query. If no arguments are passed, the currently configured type is returned.
Parameters
- 
                string|null$type optional
- the join type to be used (e.g. INNER) 
Returns
stringlink() ¶ public
link(Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities, array $options = []): boolAssociates the source entity to each of the target entities provided.
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. Changes are persisted in the database and also in the source entity.
Example:
$user = $users->get(1);
$allArticles = $articles->find('all')->toArray();
$users->Articles->link($user, $allArticles);$user->get('articles') will contain all articles in $allArticles after linking
Parameters
- 
                Cake\Datasource\EntityInterface$sourceEntity
- the row belonging to the - sourceside of this association
- 
                array$targetEntities
- list of entities belonging to the - targetside of this association
- 
                array$options optional
- list of options to be passed to the internal - savecall
Returns
booltrue on success, false otherwise
name() ¶ public
name(string|null $name = null): stringSets the name for this association. If no argument is passed then the current configured name will be returned
Parameters
- 
                string|null$name optional
- Name to be assigned 
Returns
stringproperty() ¶ public
property(string|null $name = null): stringSets the property name that should be filled with data from the target table in the source table record. If no arguments are passed, the currently configured type is returned.
Parameters
- 
                string|null$name optional
- The name of the association property. Use null to read the current value. 
Returns
stringreplace() ¶ public
replace(Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities, array $options = []): boolReplaces 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 author has many articles, such as 'article1','article 2' and 'article 3' and you pass to this method an array containing the entities for articles 'article 1' and 'article 4', only the link for 'article 1' will be kept in database, the links for 'article 2' and 'article 3' will be deleted and the link for 'article 4' will be created.
Existing links are not deleted and created again, they are either left untouched or updated.
This method does not check link uniqueness.
On success, the passed $sourceEntity will contain $targetEntities as  value
in the corresponding property for this association.
Additional options for new links to be saved can be passed in the third argument,
check Table::save() for information on the accepted options.
Example:
$author->articles = [$article1, $article2, $article3, $article4];
$authors->save($author);
$articles = [$article1, $article3];
$authors->association('articles')->replace($author, $articles);$author->get('articles') will contain only [$article1, $article3] at the end
Parameters
- 
                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$options optional
- list of options to be passed to the internal - save/- deletecalls when persisting/updating new links, or deleting existing ones
Returns
boolsuccess
Throws
InvalidArgumentExceptionif non persisted entities are passed or if any of them is lacking a primary key value
requiresKeys() ¶ public
requiresKeys(array $options = []): boolReturns 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.
Parameters
- 
                array$options optional
- The options containing the strategy to be used. 
Returns
booltrue if a list of keys will be required
saveAssociated() ¶ public
saveAssociated(Cake\Datasource\EntityInterface $entity, array|ArrayObject $options = []): bool|Cake\Datasource\EntityInterfaceTakes 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
Parameters
- 
                Cake\Datasource\EntityInterface$entity
- an entity from the source table 
- 
                array|ArrayObject$options optional
- options to be passed to the save method in the target table 
Returns
bool|Cake\Datasource\EntityInterfacefalse if $entity could not be saved, otherwise it returns the saved entity
Throws
InvalidArgumentExceptionwhen the association data cannot be traversed.
See Also
saveStrategy() ¶ public
saveStrategy(string|null $strategy = null): stringSets the strategy that should be used for saving. If called with no arguments, it will return the currently configured strategy
Parameters
- 
                string|null$strategy optional
- the strategy name to be used 
Returns
stringthe strategy to be used for saving
Throws
InvalidArgumentExceptionif an invalid strategy name is passed
sort() ¶ public
sort(mixed $sort = null): mixedSets the sort order in which target records should be returned. If no arguments are passed the currently configured value is returned
Parameters
- 
                mixed$sort optional
- A find() compatible order clause 
Returns
mixedsource() ¶ public
source(Cake\ORM\Table|null $table = null): Cake\ORM\TableSets the table instance for the source side of the association. If no arguments are passed, the current configured table instance is returned
Parameters
- 
                Cake\ORM\Table|null$table optional
- the instance to be assigned as source side 
Returns
Cake\ORM\Tablestrategy() ¶ public
strategy(string|null $name = null): stringSets 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. If no arguments are passed, the currently configured strategy is returned.
Parameters
- 
                string|null$name optional
- The strategy type. Use null to read the current value. 
Returns
stringThrows
InvalidArgumentExceptionWhen an invalid strategy is provided.
tableLocator() ¶ public
tableLocator(Cake\ORM\Locator\LocatorInterface|null $tableLocator = null): Cake\ORM\Locator\LocatorInterfaceSets the table locator. If no parameters are passed, it will return the currently used locator.
Parameters
- 
                Cake\ORM\Locator\LocatorInterface|null$tableLocator optional
- LocatorInterface instance. 
Returns
Cake\ORM\Locator\LocatorInterfacetarget() ¶ public
target(Cake\ORM\Table|null $table = null): Cake\ORM\TableSets the table instance for the target side of the association. If no arguments are passed, the current configured table instance is returned
Parameters
- 
                Cake\ORM\Table|null$table optional
- the instance to be assigned as target side 
Returns
Cake\ORM\TabletransformRow() ¶ public
transformRow(array $row, string $nestKey, bool $joined, string $targetProperty = null): arrayCorrectly nests a result row associated values into the correct array keys inside the source results.
Parameters
- 
                array$row
- The row to transform 
- 
                string$nestKey
- The array key under which the results for this association should be found 
- 
                bool$joined
- Whether or not the row is a result of a direct join with this association 
- 
                string$targetProperty optional
- The property name in the source results where the association data should be nested in. Will use the default one if not provided. 
Returns
arrayunlink() ¶ public
unlink(Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities, array $options = []): voidRemoves 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.
Options
Additionally to the default options accepted by Table::delete(), the following
keys are supported:
- cleanProperty: Whether or not to remove all the objects in $targetEntitiesthat are stored in$sourceEntity(default: true)
By default this method will unset each of the entity objects stored inside the source entity.
Changes are persisted in the database and also in the source entity.
Example:
$user = $users->get(1);
$user->articles = [$article1, $article2, $article3, $article4];
$users->save($user, ['Associated' => ['Articles']]);
$allArticles = [$article1, $article2, $article3];
$users->Articles->unlink($user, $allArticles);$article->get('articles') will contain only [$article4] after deleting in the database
Parameters
- 
                Cake\Datasource\EntityInterface$sourceEntity
- an entity persisted in the source table for this association 
- 
                array$targetEntities
- list of entities persisted in the target table for this association 
- 
                array$options optional
- list of options to be passed to the internal - deletecall
Returns
voidThrows
InvalidArgumentExceptionif non persisted entities are passed or if any of them is lacking a primary key value
updateAll() ¶ public
updateAll(array $fields, mixed $conditions): boolProxies the update operation to the target table's updateAll method
Parameters
- 
                array$fields
- A hash of field => new value. 
- 
                mixed$conditions
- Conditions to be used, accepts anything Query::where() can take. 
Returns
boolSuccess Returns true if one or more rows are affected.
See Also
Property Detail
$_bindingKey ¶ protected
The field name in the owning side table that is used to match with the foreignKey
Type
string|array$_cascadeCallbacks ¶ protected
Whether or not cascaded deletes should also fire callbacks.
Type
bool$_conditions ¶ protected
A list of conditions to be always included when fetching records from the target association
Type
array$_dependent ¶ protected
Whether 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.
Type
bool$_finder ¶ protected
The default finder name to use for fetching rows from the target table
Type
string$_foreignKey ¶ protected
The name of the field representing the foreign key to the table to load
Type
string|array$_joinType ¶ protected
The type of join to be used when adding the association to a query
Type
string$_name ¶ protected
Name given to the association, it usually represents the alias assigned to the target associated table
Type
string$_propertyName ¶ protected
The property name that should be filled with data from the target table in the source table record.
Type
string