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.
- Cake\ORM\Association uses Cake\Core\ConventionsTrait , Cake\ORM\Locator\LocatorAwareTrait
-
Cake\ORM\Association\HasMany
Constants summary
-
string
'append'
-
string
'replace'
Inherited Constants
-
MANY_TO_MANY
,MANY_TO_ONE
,ONE_TO_MANY
,ONE_TO_ONE
,STRATEGY_JOIN
,STRATEGY_SELECT
,STRATEGY_SUBQUERY
Properties summary
-
$_joinType
protectedThe type of join to be used when adding the association to a querystring
-
$_saveStrategy
protectedSaving strategy to be used by this associationstring
-
$_sort
protectedOrder in which target records should be returnedmixed
-
$_strategy
protectedThe strategy name to be used to fetch associated records.string
-
$_validStrategies
protectedValid strategies for this type of associationarray
Inherited Properties
-
_bindingKey
,_cascadeCallbacks
,_className
,_conditions
,_dependent
,_finder
,_foreignKey
,_name
,_propertyName
,_sourceTable
,_targetTable
_tableLocator
Method Summary
- _foreignKeyAcceptsNull() protectedChecks the nullable flag of the foreign key
- _options() protectedParse extra options passed in the constructor.
- _saveTarget() protected
Persists each of the entities into the target table and creates links between the parent entity and each one of the saved target entities.
- _unlink() protected
Deletes/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() protected
Deletes/sets null the related objects according to the dependency between source and targets and foreign key nullability Skips deleting records present in $remainingEntities
- canBeJoined() publicWhether this association can be expressed directly in a query join
- cascadeDelete() publicHandles cascading a delete from an associated model.
- defaultRowValue() public
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.
- eagerLoader() public
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 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.
- getForeignKey() publicGets the name of the field representing the foreign key to the source table.
- getSaveStrategy() publicGets the strategy that should be used for saving.
- getSort() publicGets the sort order in which target records should be returned.
- isOwningSide() public
Returns 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.
- link() public
Associates 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. - replace() public
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. - saveAssociated() public
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
- saveStrategy() public
Sets the strategy that should be used for saving. If called with no arguments, it will return the currently configured strategy
- setSaveStrategy() publicSets the strategy that should be used for saving.
- setSort() publicSets the sort order in which target records should be returned.
- sort() public
Sets the sort order in which target records should be returned. If no arguments are passed the currently configured value is returned
- type() publicGet the relationship type.
- unlink() public
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.
Method Detail
_foreignKeyAcceptsNull()source protected
_foreignKeyAcceptsNull( Cake\ORM\Table $table , array $properties )
Checks 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
boolean_options()source protected
_options( array $opts )
Parse extra options passed in the constructor.
Parameters
- array
$opts
- original list of options passed in constructor
Overrides
Cake\ORM\Association::_options()
_saveTarget()source protected
_saveTarget( array $foreignKeyReference , Cake\Datasource\EntityInterface $parentEntity , array|Traversable $entities , array $options )
Persists each of the entities into the target table and creates links between the parent entity and each one of the saved target entities.
Parameters
- array
$foreignKeyReference
The foreign key reference defining the link between the target entity, and the parent entity.
-
Cake\Datasource\EntityInterface
$parentEntity
The source entity containing the target entities to be saved.
- array|Traversable
$entities
list of entities to persist in target table and to link to the parent entity
- array
$options
- list of options accepted by
Table::save()
.
Returns
booleantrue
on success, false
otherwise._unlink()source protected
_unlink( array $foreignKey , Cake\ORM\Table $target , array $conditions [] , array $options [] )
Deletes/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
booleansuccess
_unlinkAssociated()source protected
_unlinkAssociated( array $foreignKeyReference , Cake\Datasource\EntityInterface $entity , Cake\ORM\Table $target , array $remainingEntities [] , array $options [] )
Deletes/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
$foreignKeyReference
The foreign key reference defining the link between the target entity, and the parent entity.
-
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
booleansuccess
canBeJoined()source public
canBeJoined( array $options [] )
Whether this association can be expressed directly in a query join
Parameters
- array
$options
optional [] - custom options key that could alter the return value
Returns
booleanif the 'matching' key in $option is true then this function will return true, false otherwise
Overrides
Cake\ORM\Association::canBeJoined()
cascadeDelete()source public
cascadeDelete( Cake\Datasource\EntityInterface $entity , array $options [] )
Handles cascading a delete from an associated model.
Parameters
-
Cake\Datasource\EntityInterface
$entity
- The entity that started the cascaded delete.
- array
$options
optional [] - The options for the original delete.
Returns
booleanSuccess
defaultRowValue()source public
defaultRowValue( array $row , boolean $joined )
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.
Parameters
- array
$row
- The row to set a default on.
- boolean
$joined
Whether or not the row is a result of a direct join with this association
Returns
arrayOverrides
Cake\ORM\Association::defaultRowValue()
eagerLoader()source public
eagerLoader( array $options )
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 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.
Parameters
- array
$options
- The options for eager loading.
Returns
callablegetForeignKey()source public
getForeignKey( )
Gets the name of the field representing the foreign key to the source table.
Returns
stringOverrides
Cake\ORM\Association::getForeignKey()
getSaveStrategy()source public
getSaveStrategy( )
Gets the strategy that should be used for saving.
Returns
stringthe strategy to be used for saving
getSort()source public
getSort( )
Gets the sort order in which target records should be returned.
Returns
mixedisOwningSide()source public
isOwningSide( Cake\ORM\Table $side )
Returns 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
booleanlink()source public
link( Cake\Datasource\EntityInterface $sourceEntity , array $targetEntities , array $options [] )
Associates 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
source
side of this association- array
$targetEntities
list of entities belonging to the
target
side of this association- array
$options
optional [] - list of options to be passed to the internal
save
call
Returns
booleantrue on success, false otherwise
replace()source public
replace( Cake\Datasource\EntityInterface $sourceEntity , array $targetEntities , array $options [] )
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 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
/delete
calls when persisting/updating new links, or deleting existing ones
Returns
booleansuccess
Throws
InvalidArgumentExceptionif non persisted entities are passed or if any of them is lacking a primary key value
saveAssociated()source public
saveAssociated( Cake\Datasource\EntityInterface $entity , array $options [] )
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
Parameters
-
Cake\Datasource\EntityInterface
$entity
- an entity from the source table
- array
$options
optional [] options to be passed to the save method in the target table
Returns
boolean|Cake\Datasource\EntityInterface
false if $entity could not be saved, otherwise it returns the saved entity
Throws
InvalidArgumentExceptionwhen the association data cannot be traversed.
See
\Cake\ORM\Table::save()saveStrategy()source public
saveStrategy( string|null $strategy null )
Sets the strategy that should be used for saving. If called with no arguments, it will return the currently configured strategy
Deprecated
3.4.0 Use setSaveStrategy()/getSaveStrategy() instead.Parameters
- string|null
$strategy
optional null - the strategy name to be used
Returns
stringthe strategy to be used for saving
Throws
InvalidArgumentExceptionif an invalid strategy name is passed
setSaveStrategy()source public
setSaveStrategy( string $strategy )
Sets the strategy that should be used for saving.
Parameters
- string
$strategy
- the strategy name to be used
Returns
$this
Throws
InvalidArgumentExceptionif an invalid strategy name is passed
setSort()source public
setSort( mixed $sort )
Sets the sort order in which target records should be returned.
Parameters
- mixed
$sort
- A find() compatible order clause
Returns
$this
sort()source public
sort( mixed $sort null )
Sets the sort order in which target records should be returned. If no arguments are passed the currently configured value is returned
Deprecated
3.4.0 Use setSort()/getSort() instead.Parameters
- mixed
$sort
optional null - A find() compatible order clause
Returns
mixedunlink()source public
unlink( Cake\Datasource\EntityInterface $sourceEntity , array $targetEntities , array $options [] )
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.
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
$targetEntities
that 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
delete
call
Throws
InvalidArgumentExceptionif non persisted entities are passed or if any of them is lacking a primary key value
Methods inherited from Cake\ORM\Association
__call()source public
__call( string $method , array $argument )
Proxies 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()source 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()source public
__get( string $property )
Proxies property retrieval to the target table. This is handy for getting this association's associations
Parameters
- string
$property
- the property name
Returns
Cake\ORM\Association
Throws
RuntimeExceptionif no association with such name exists
__isset()source public
__isset( string $property )
Proxies 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
booleantrue if the property exists
_appendFields()source protected
_appendFields( Cake\ORM\Query $query , Cake\ORM\Query $surrogate , array $options )
Helper 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
_appendNotMatching()source protected
_appendNotMatching( Cake\Datasource\QueryInterface $query , array $options )
Conditionally 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
negateMatch
key.
_bindNewAssociations()source protected
_bindNewAssociations( Cake\ORM\Query $query , Cake\ORM\Query $surrogate , array $options )
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.
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
_dispatchBeforeFind()source protected
_dispatchBeforeFind( Cake\ORM\Query $query )
Triggers 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
_extractFinder()source protected
_extractFinder( string|array $finderData )
Helper 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_formatAssociationResults()source protected
_formatAssociationResults( Cake\ORM\Query $query , Cake\ORM\Query $surrogate , array $options )
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.
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
_getClassName()source protected
_getClassName( string $alias , array $options [] )
Gets the table class name.
Parameters
- string
$alias
- The alias name you want to get.
- array
$options
optional [] - Table options array.
Returns
string_joinCondition()source protected
_joinCondition( array $options )
Returns 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
_propertyName()source protected
_propertyName( )
Returns default property name based on association name.
Returns
stringattachTo()source public
attachTo( Cake\ORM\Query $query , array $options [] )
Alters 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
Throws
RuntimeExceptionif the query builder passed does not return a query object
bindingKey()source public
bindingKey( string|null $key null )
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.
If no parameters are passed the current field is returned
Deprecated
3.4.0 Use setBindingKey()/getBindingKey() instead.Parameters
- string|null
$key
optional null - the table field to be used to link both tables together
Returns
string|arraycascadeCallbacks()source public
cascadeCallbacks( boolean|null $cascadeCallbacks null )
Sets whether or not cascaded deletes should also fire callbacks. If no arguments are passed, the current configured value is returned
Deprecated
3.4.0 Use setCascadeCallbacks()/getCascadeCallbacks() instead.Parameters
- boolean|null
$cascadeCallbacks
optional null - cascade callbacks switch value
Returns
booleanconditions()source public
conditions( array|null $conditions null )
Sets 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
Deprecated
3.4.0 Use setConditions()/getConditions() instead.Parameters
- array|null
$conditions
optional null - list of conditions to be used
Returns
arraySee
\Cake\Database\Query::where() for examples on the format of the arraydeleteAll()source public
deleteAll( mixed $conditions )
Proxies the delete operation to the target table's deleteAll method
Parameters
- mixed
$conditions
Conditions to be used, accepts anything Query::where() can take.
Returns
integerReturns the number of affected rows.
See
\Cake\ORM\Table::deleteAll()dependent()source public
dependent( boolean|null $dependent null )
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.
Deprecated
3.4.0 Use setDependent()/getDependent() instead.Parameters
- boolean|null
$dependent
optional null - Set the dependent mode. Use null to read the current state.
Returns
booleanexists()source public
exists( array|callable|Cake\Database\ExpressionInterface $conditions )
Proxies 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
booleanSee
\Cake\ORM\Table::exists()find()source public
find( string|array|null $type null , array $options [] )
Proxies 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 null the type of query to perform, if an array is passed, it will be interpreted as the
$options
parameter- array
$options
optional [] - The options to for the find
Returns
Cake\ORM\Query
See
\Cake\ORM\Table::find()finder()source public
finder( string|null $finder null )
Sets 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.
Deprecated
3.4.0 Use setFinder()/getFinder() instead.Parameters
- string|null
$finder
optional null - the finder name to use
Returns
stringforeignKey()source public
foreignKey( string|null $key null )
Sets the name of the field representing the foreign key to the target table. If no parameters are passed the current field is returned
Deprecated
3.4.0 Use setForeignKey()/getForeignKey() instead.Parameters
- string|null
$key
optional null - the key to be used to link both tables together
Returns
string|arraygetBindingKey()source public
getBindingKey( )
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.
Returns
string|arraygetCascadeCallbacks()source public
getCascadeCallbacks( )
Gets whether or not cascaded deletes should also fire callbacks.
Returns
booleangetConditions()source public
getConditions( )
Gets a list of conditions to be always included when fetching records from the target association.
Returns
arraySee
\Cake\Database\Query::where() for examples on the format of the arraygetDependent()source public
getDependent( )
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.
Returns
booleangetFinder()source public
getFinder( )
Gets the default finder to use for fetching rows from the target table.
Returns
stringgetJoinType()source public
getJoinType( )
Gets the type of join to be used when adding the association to a query.
Returns
stringgetName()source public
getName( )
Gets the name for this association, usually the alias assigned to the target associated table
Returns
stringgetProperty()source public
getProperty( )
Gets the property name that should be filled with data from the target table in the source table record.
Returns
stringgetSource()source public
getSource( )
Gets the table instance for the source side of the association.
Returns
Cake\ORM\Table
getStrategy()source public
getStrategy( )
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.
Returns
stringgetTarget()source public
getTarget( )
Gets the table instance for the target side of the association.
Returns
Cake\ORM\Table
joinType()source public
joinType( string|null $type null )
Sets 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.
Deprecated
3.4.0 Use setJoinType()/getJoinType() instead.Parameters
- string|null
$type
optional null - the join type to be used (e.g. INNER)
Returns
stringname()source public
name( string|null $name null )
Sets the name for this association.
Deprecated
3.4.0 Use setName()/getName() instead.Parameters
- string|null
$name
optional null - Name to be assigned
Returns
stringproperty()source public
property( string|null $name null )
Sets 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.
Deprecated
3.4.0 Use setProperty()/getProperty() instead.Parameters
- string|null
$name
optional null - The name of the association property. Use null to read the current value.
Returns
stringrequiresKeys()source public
requiresKeys( array $options [] )
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.
Parameters
- array
$options
optional [] - The options containing the strategy to be used.
Returns
booleantrue if a list of keys will be required
setBindingKey()source public
setBindingKey( string|array $key )
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.
Parameters
- string|array
$key
- the table field or fields to be used to link both tables together
Returns
$this
setCascadeCallbacks()source public
setCascadeCallbacks( boolean $cascadeCallbacks )
Sets whether or not cascaded deletes should also fire callbacks.
Parameters
- boolean
$cascadeCallbacks
- cascade callbacks switch value
Returns
$this
setConditions()source public
setConditions( array $conditions )
Sets a list of conditions to be always included when fetching records from the target association.
Parameters
- array
$conditions
- list of conditions to be used
Returns
$this
See
\Cake\Database\Query::where() for examples on the format of the arraysetDependent()source public
setDependent( boolean $dependent )
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.
Parameters
- boolean
$dependent
- Set the dependent mode. Use null to read the current state.
Returns
$this
setFinder()source public
setFinder( string $finder )
Sets the default finder to use for fetching rows from the target table.
Parameters
- string
$finder
- the finder name to use
Returns
$this
setForeignKey()source public
setForeignKey( string|array $key )
Sets the name of the field representing the foreign key to the target table.
Parameters
- string|array
$key
- the key or keys to be used to link both tables together
Returns
$this
setJoinType()source public
setJoinType( string $type )
Sets the type of join to be used when adding the association to a query.
Parameters
- string
$type
- the join type to be used (e.g. INNER)
Returns
$this
setName()source public
setName( string $name )
Sets the name for this association, usually the alias assigned to the target associated table
Parameters
- string
$name
- Name to be assigned
Returns
$this
setProperty()source public
setProperty( string $name )
Sets the property name that should be filled with data from the target table in the source table record.
Parameters
- string
$name
- The name of the association property. Use null to read the current value.
Returns
$this
setSource()source public
setSource( Cake\ORM\Table $table )
Sets the table instance for the source side of the association.
Parameters
-
Cake\ORM\Table
$table
- the instance to be assigned as source side
Returns
$this
setStrategy()source public
setStrategy( string $name )
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.
Parameters
- string
$name
- The strategy type. Use null to read the current value.
Returns
$this
Throws
InvalidArgumentExceptionWhen an invalid strategy is provided.
setTarget()source public
setTarget( Cake\ORM\Table $table )
Sets the table instance for the target side of the association.
Parameters
-
Cake\ORM\Table
$table
- the instance to be assigned as target side
Returns
$this
source()source public
source( Cake\ORM\Table $table null )
Sets the table instance for the source side of the association. If no arguments are passed, the current configured table instance is returned
Deprecated
3.4.0 Use setSource()/getSource() instead.Parameters
-
Cake\ORM\Table
$table
optional null - the instance to be assigned as source side
Returns
Cake\ORM\Table
strategy()source public
strategy( string|null $name null )
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. If no arguments are passed, the currently configured strategy is returned.
Deprecated
3.4.0 Use setStrategy()/getStrategy() instead.Parameters
- string|null
$name
optional null - The strategy type. Use null to read the current value.
Returns
stringThrows
InvalidArgumentExceptionWhen an invalid strategy is provided.
target()source public
target( Cake\ORM\Table $table null )
Sets the table instance for the target side of the association. If no arguments are passed, the current configured table instance is returned
Deprecated
3.4.0 Use setTarget()/getTarget() instead.Parameters
-
Cake\ORM\Table
$table
optional null - the instance to be assigned as target side
Returns
Cake\ORM\Table
transformRow()source public
transformRow( array $row , string $nestKey , boolean $joined , string|null $targetProperty null )
Correctly 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
- boolean
$joined
Whether or not the row is a result of a direct join with this association
- string|null
$targetProperty
optional null The property name in the source results where the association data shuld be nested in. Will use the default one if not provided.
Returns
arrayupdateAll()source public
updateAll( array $fields , mixed $conditions )
Proxies 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
booleanSuccess Returns true if one or more rows are affected.
See
\Cake\ORM\Table::updateAll()Methods used from Cake\Core\ConventionsTrait
_camelize()source protected
_camelize( string $name )
Creates a camelized version of $name
Parameters
- string
$name
- name
Returns
stringCamelized name
_entityName()source protected
_entityName( string $name )
Creates the proper entity name (singular) for the specified name
Parameters
- string
$name
- Name
Returns
stringCamelized and plural model name
_fixtureName()source protected
_fixtureName( string $name )
Creates a fixture name
Parameters
- string
$name
- Model class name
Returns
stringSingular model key
_modelKey()source protected
_modelKey( string $name )
Creates 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()source protected
_modelNameFromKey( string $key )
Creates the proper model name from a foreign key
Parameters
- string
$key
- Foreign key
Returns
stringModel name
_pluginNamespace()source protected
_pluginNamespace( string $pluginName )
Return plugin's namespace
Parameters
- string
$pluginName
- Plugin name
Returns
stringPlugin's namespace
_pluginPath()source protected
_pluginPath( string $pluginName )
Find 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()source protected
_pluralHumanName( string $name )
Creates the plural human name used in views
Parameters
- string
$name
- Controller name
Returns
stringPlural human name
_singularHumanName()source protected
_singularHumanName( string $name )
Creates the singular human name used in views
Parameters
- string
$name
- Controller name
Returns
stringSingular human name
_singularName()source protected
_singularName( string $name )
Creates the singular name for use in views.
Parameters
- string
$name
- Name to use
Returns
stringVariable name
_variableName()source protected
_variableName( string $name )
Creates the plural variable name for views
Parameters
- string
$name
- Name to use
Returns
stringPlural name for views
Methods used from Cake\ORM\Locator\LocatorAwareTrait
getTableLocator()source public
getTableLocator( )
Gets the table locator.
Returns
Cake\ORM\Locator\LocatorInterface
setTableLocator()source public
setTableLocator( Cake\ORM\Locator\LocatorInterface $tableLocator )
Sets the table locator.
Parameters
-
Cake\ORM\Locator\LocatorInterface
$tableLocator
- LocatorInterface instance.
Returns
$this
tableLocator()source public
tableLocator( Cake\ORM\Locator\LocatorInterface $tableLocator null )
Sets the table locator. If no parameters are passed, it will return the currently used locator.
Deprecated
3.5.0 Use getTableLocator()/setTableLocator() instead.Parameters
-
Cake\ORM\Locator\LocatorInterface
$tableLocator
optional null - LocatorInterface instance.
Returns
Cake\ORM\Locator\LocatorInterface
Properties detail
$_joinTypesource
protected string
The type of join to be used when adding the association to a query
'INNER'
$_saveStrategysource
protected string
Saving strategy to be used by this association
self::SAVE_APPEND
$_strategysource
protected string
The strategy name to be used to fetch associated records.
self::STRATEGY_SELECT
$_validStrategiessource
protected array
Valid strategies for this type of association
[ self::STRATEGY_SELECT, self::STRATEGY_SUBQUERY ]
© 2005–2017 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/3.4/class-Cake.ORM.Association.HasMany.html