Class EagerLoader
Exposes the methods for storing the associations that should be eager loaded for a table once a query is provided and delegates the job of creating the required joins and decorating the results so that those associations can be part of the result set.
Property Summary
-
$aliasList protected
array<string, array<string, array<int, \Cake\ORM\EagerLoadable>>>Contains a list of the association names that are to be eagerly loaded.
-
$autoFields protected
boolControls whether fields from associated tables will be eagerly loaded. When set to false, no fields will be loaded from associations.
-
$containOptions protected
array<string, int>List of options accepted by associations in contain() index by key for faster access.
-
$containments protected
array<string, array>Nested array describing the association to be fetched and the options to apply for each of them, if any
-
$joinsMap protected
array<string, \Cake\ORM\EagerLoadable>A map of table aliases pointing to the association objects they represent for the query.
-
$loadExternal protected
array<int, \Cake\ORM\EagerLoadable>A list of associations that should be loaded with a separate query.
-
$matching protected
Cake\ORM\EagerLoader|nullAnother EagerLoader instance that will be used for 'matching' associations.
-
$normalized protected
array<string, \Cake\ORM\EagerLoadable>|nullContains a nested array with the compiled containments tree This is a normalized version of the user provided containments array.
Method Summary
-
__clone() public
Handles cloning eager loaders and eager loadables.
-
addToJoinsMap() public
Registers a table alias, typically loaded as a join in a query, as belonging to an association. This helps hydrators know what to do with the columns coming from such joined table.
-
associationsMap() public
Returns an array having as keys a dotted path of associations that participate in this eager loader. The values of the array will contain the following keys:
alias: The association aliasinstance: The association instancecanBeJoined: Whether the association will be loaded using a JOINentityClass: The entity that should be used for hydrating the resultsnestKey: A dotted path that can be used to correctly insert the data into the results.matching: Whether it is an association loaded throughmatching().
-
attachAssociations() public
Modifies the passed query to apply joins or any other transformation required in order to eager load the associations described in the
containarray. This method will not modify the query for loading external associations, i.e. those that cannot be loaded without executing a separate query. -
attachableAssociations() public
Returns an array with the associations that can be fetched using a single query, the array keys are the association aliases and the values will contain an array with Cake\ORM\EagerLoadable objects.
-
buildAssociationsMap() protected
An internal method to build a map which is used for the return value of the associationsMap() method.
-
clearContain() public
Remove any existing non-matching based containments.
This will reset/clear out any contained associations that were not added via matching().
-
collectKeys() protected
Helper function used to return the keys from the query records that will be used to eagerly load associations.
-
contain() public
Sets the list of associations that should be eagerly loaded along for a specific table using when a query is provided. The list of associated tables passed to this method must have been previously set as associations using the Table API.
Associations can be arbitrarily nested using dot notation or nested arrays, this allows this object to calculate joins or any additional queries that must be executed to bring the required associated data.
Accepted options per passed association:
foreignKey: Used to set a different field to match both tables, if set to false no join conditions will be generated automaticallyfields: An array with the fields that should be fetched from the associationqueryBuilder: Equivalent to passing a callback instead of an options arraymatching: Whether to inform the association class that it should filter the main query by the results fetched by that class.joinType: For joinable associations, the SQL join type to use.strategy: The loading strategy to use (join, select, subquery)
-
correctStrategy() protected
Changes the association fetching strategy if required because of duplicate under the same direct associations chain.
-
correctSubqueryStrategy() protected
Switches associations away from the subquery strategy when the subquery filter would not describe the rows the query returned.
The filter groups by the source binding key and inherits the query's LIMIT, so the limit cuts the distinct keys instead of the rows. Both sets only coincide while the association is owned by the query's own repository. As soon as it hangs off another association, many rows share one key and the filter returns a different, largely disjoint set of keys.
-
disableAutoFields() public
Disable auto loading fields of contained associations.
-
enableAutoFields() public
Sets whether contained associations will load fields automatically.
-
externalAssociations() public
Returns an array with the associations that need to be fetched using a separate query, each array value will contain a {@link \Cake\ORM\EagerLoadable} object.
-
fixStrategies() protected
Iterates over the joinable aliases list and corrects the fetching strategies in order to avoid aliases collision in the generated queries.
This function operates on the array references that were generated by the _normalizeContain() function.
-
getContain() public
Gets the list of associations that should be eagerly loaded along for a specific table using when a query is provided. The list of associated tables passed to this method must have been previously set as associations using the Table API.
-
getMatching() public
Returns the current tree of associations to be matched.
-
groupKeys() protected
Helper function used to iterate a statement and extract the columns defined in $collectKeys.
-
isAutoFieldsEnabled() public
Gets whether contained associations will load fields automatically.
-
loadExternal() public
Inject data from associations that cannot be joined directly.
-
normalizeContain() protected
Auxiliary function responsible for fully normalizing deep associations defined using
contain(). -
normalized() public
Returns the fully normalized array of associations that should be eagerly loaded for a table. The normalized array will restructure the original array by sorting all associations under one key and special options under another.
Each of the levels of the associations tree will be converted to a {@link \Cake\ORM\EagerLoadable} object, that contains all the information required for the association objects to load the information from the database.
Additionally, it will set an 'instance' key per association containing the association instance from the corresponding source table
-
reformatContain() protected
Formats the containments array so that associations are always set as keys in the array. This function merges the original associations array with the new associations provided.
-
resolveJoins() protected
Helper function used to compile a list of all associations that can be joined in the query.
-
setMatching() public
Adds a new association to the list that will be used to filter the results of any given query based on the results of finding records for that association. You can pass a dot separated path of associations to this method as its first parameter, this will translate in setting all those associations with the
matchingoption.Options
joinType: INNER, OUTER, ...fields: Fields to containnegateMatch: Whether to add conditions negate match on target association
Method Detail
addToJoinsMap() ¶ public
addToJoinsMap(string $alias, Cake\ORM\Association $assoc, bool $asMatching = false, string|null $targetProperty = null): void
Registers a table alias, typically loaded as a join in a query, as belonging to an association. This helps hydrators know what to do with the columns coming from such joined table.
Parameters
-
string$alias The table alias as it appears in the query.
-
Cake\ORM\Association$assoc The association object the alias represents; will be normalized.
-
bool$asMatching optional Whether this join results should be treated as a 'matching' association.
-
string|null$targetProperty optional The property name where the results of the join should be nested at. If not passed, the default property for the association will be used.
Returns
voidassociationsMap() ¶ public
associationsMap(Cake\ORM\Table $table): array
Returns an array having as keys a dotted path of associations that participate in this eager loader. The values of the array will contain the following keys:
alias: The association aliasinstance: The association instancecanBeJoined: Whether the association will be loaded using a JOINentityClass: The entity that should be used for hydrating the resultsnestKey: A dotted path that can be used to correctly insert the data into the results.matching: Whether it is an association loaded throughmatching().
Parameters
-
Cake\ORM\Table$table The table containing the association that will be normalized.
Returns
arrayattachAssociations() ¶ public
attachAssociations(Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $query, Cake\ORM\Table $repository, bool $includeFields): void
Modifies the passed query to apply joins or any other transformation required
in order to eager load the associations described in the contain array.
This method will not modify the query for loading external associations, i.e.
those that cannot be loaded without executing a separate query.
Parameters
-
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>$query The query to be modified.
-
Cake\ORM\Table$repository The repository containing the associations
-
bool$includeFields whether to append all fields from the associations to the passed query. This can be overridden according to the settings defined per association in the containments array.
Returns
voidattachableAssociations() ¶ public
attachableAssociations(Cake\ORM\Table $repository): array<string, \Cake\ORM\EagerLoadable>
Returns an array with the associations that can be fetched using a single query, the array keys are the association aliases and the values will contain an array with Cake\ORM\EagerLoadable objects.
Parameters
-
Cake\ORM\Table$repository The table containing the associations to be attached.
Returns
array<string, \Cake\ORM\EagerLoadable>buildAssociationsMap() ¶ protected
buildAssociationsMap(array $map, array<Cake\ORM\EagerLoadable> $level, bool $matching = false): array
An internal method to build a map which is used for the return value of the associationsMap() method.
Parameters
-
array$map An initial array for the map.
-
array<Cake\ORM\EagerLoadable>$level An array of EagerLoadable instances.
-
bool$matching optional Whether it is an association loaded through
matching().
Returns
arrayclearContain() ¶ public
clearContain(): void
Remove any existing non-matching based containments.
This will reset/clear out any contained associations that were not added via matching().
Returns
voidcollectKeys() ¶ protected
collectKeys(array<Cake\ORM\EagerLoadable> $external, Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $query, array $results): array
Helper function used to return the keys from the query records that will be used to eagerly load associations.
Parameters
-
array<Cake\ORM\EagerLoadable>$external The list of external associations to be loaded.
-
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>$query The query from which the results where generated.
-
array$results Results array.
Returns
arraycontain() ¶ public
contain(array|string $associations, Closure|null $queryBuilder = null): array
Sets the list of associations that should be eagerly loaded along for a specific table using when a query is provided. The list of associated tables passed to this method must have been previously set as associations using the Table API.
Associations can be arbitrarily nested using dot notation or nested arrays, this allows this object to calculate joins or any additional queries that must be executed to bring the required associated data.
Accepted options per passed association:
foreignKey: Used to set a different field to match both tables, if set to false no join conditions will be generated automaticallyfields: An array with the fields that should be fetched from the associationqueryBuilder: Equivalent to passing a callback instead of an options arraymatching: Whether to inform the association class that it should filter the main query by the results fetched by that class.joinType: For joinable associations, the SQL join type to use.strategy: The loading strategy to use (join, select, subquery)
Parameters
-
array|string$associations List of table aliases to be queried. When this method is called multiple times it will merge previous list with the new one.
-
Closure|null$queryBuilder optional The query builder callback.
Returns
arrayContainments.
Throws
InvalidArgumentExceptionWhen using $queryBuilder with an array of $associations
correctStrategy() ¶ protected
correctStrategy(Cake\ORM\EagerLoadable $loadable): void
Changes the association fetching strategy if required because of duplicate under the same direct associations chain.
Parameters
-
Cake\ORM\EagerLoadable$loadable The association config.
Returns
voidcorrectSubqueryStrategy() ¶ protected
correctSubqueryStrategy(array<Cake\ORM\EagerLoadable> $external, Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $query): void
Switches associations away from the subquery strategy when the subquery filter would not describe the rows the query returned.
The filter groups by the source binding key and inherits the query's LIMIT, so the limit cuts the distinct keys instead of the rows. Both sets only coincide while the association is owned by the query's own repository. As soon as it hangs off another association, many rows share one key and the filter returns a different, largely disjoint set of keys.
Parameters
-
array<Cake\ORM\EagerLoadable>$external The list of external associations to be loaded.
-
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>$query The query the associations are loaded for.
Returns
voiddisableAutoFields() ¶ public
disableAutoFields(): $this
Disable auto loading fields of contained associations.
Returns
$thisenableAutoFields() ¶ public
enableAutoFields(bool $enable = true): $this
Sets whether contained associations will load fields automatically.
Parameters
-
bool$enable optional The value to set.
Returns
$thisexternalAssociations() ¶ public
externalAssociations(Cake\ORM\Table $repository): array<Cake\ORM\EagerLoadable>
Returns an array with the associations that need to be fetched using a separate query, each array value will contain a {@link \Cake\ORM\EagerLoadable} object.
Parameters
-
Cake\ORM\Table$repository The table containing the associations to be loaded.
Returns
array<Cake\ORM\EagerLoadable>fixStrategies() ¶ protected
fixStrategies(): void
Iterates over the joinable aliases list and corrects the fetching strategies in order to avoid aliases collision in the generated queries.
This function operates on the array references that were generated by the _normalizeContain() function.
Returns
voidgetContain() ¶ public
getContain(): array
Gets the list of associations that should be eagerly loaded along for a specific table using when a query is provided. The list of associated tables passed to this method must have been previously set as associations using the Table API.
Returns
arrayContainments.
getMatching() ¶ public
getMatching(): array
Returns the current tree of associations to be matched.
Returns
arrayThe resulting containments array.
groupKeys() ¶ protected
groupKeys(array $results, array<string, array{0: string, 1: array<string>, 2: bool}> $collectKeys): array
Helper function used to iterate a statement and extract the columns defined in $collectKeys.
Parameters
-
array$results Results array.
-
array<string, array{0: string, 1: array<string>, 2: bool}>$collectKeys The keys to collect.
Returns
arrayisAutoFieldsEnabled() ¶ public
isAutoFieldsEnabled(): bool
Gets whether contained associations will load fields automatically.
Returns
boolThe current value.
loadExternal() ¶ public
loadExternal(Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $query, iterable $results): iterable
Inject data from associations that cannot be joined directly.
Parameters
-
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>$query The query for which to eager load external. associations.
-
iterable$results Results.
Returns
iterableThrows
RuntimeExceptionnormalizeContain() ¶ protected
normalizeContain(Cake\ORM\Table $parent, string $alias, array<string, mixed> $options, array<string, mixed> $paths): Cake\ORM\EagerLoadable
Auxiliary function responsible for fully normalizing deep associations defined
using contain().
Parameters
-
Cake\ORM\Table$parent Owning side of the association.
-
string$alias Name of the association to be loaded.
-
array<string, mixed>$options List of extra options to use for this association.
-
array<string, mixed>$paths An array with two values, the first one is a list of dot separated strings representing associations that lead to this
$aliasin the chain of associations to be loaded. The second value is the path to follow in entities' properties to fetch a record of the corresponding association.
Returns
Cake\ORM\EagerLoadableObject with normalized associations
Throws
InvalidArgumentExceptionWhen containments refer to associations that do not exist.
normalized() ¶ public
normalized(Cake\ORM\Table $repository): array<string, \Cake\ORM\EagerLoadable>
Returns the fully normalized array of associations that should be eagerly loaded for a table. The normalized array will restructure the original array by sorting all associations under one key and special options under another.
Each of the levels of the associations tree will be converted to a {@link \Cake\ORM\EagerLoadable} object, that contains all the information required for the association objects to load the information from the database.
Additionally, it will set an 'instance' key per association containing the association instance from the corresponding source table
Parameters
-
Cake\ORM\Table$repository The table containing the association that will be normalized.
Returns
array<string, \Cake\ORM\EagerLoadable>reformatContain() ¶ protected
reformatContain(array $associations, array $original): array<string, array>
Formats the containments array so that associations are always set as keys in the array. This function merges the original associations array with the new associations provided.
Parameters
-
array$associations User provided containments array.
-
array$original The original containments array to merge with the new one.
Returns
array<string, array>resolveJoins() ¶ protected
resolveJoins(array<string, \Cake\ORM\EagerLoadable> $associations, array<string, \Cake\ORM\EagerLoadable> $matching = []): array<string, \Cake\ORM\EagerLoadable>
Helper function used to compile a list of all associations that can be joined in the query.
Parameters
-
array<string, \Cake\ORM\EagerLoadable>$associations List of associations from which to obtain joins.
-
array<string, \Cake\ORM\EagerLoadable>$matching optional List of associations that should be forcibly joined.
Returns
array<string, \Cake\ORM\EagerLoadable>setMatching() ¶ public
setMatching(string $associationPath, Closure|null $builder = null, array<string, mixed> $options = []): $this
Adds a new association to the list that will be used to filter the results of
any given query based on the results of finding records for that association.
You can pass a dot separated path of associations to this method as its first
parameter, this will translate in setting all those associations with the
matching option.
Options
joinType: INNER, OUTER, ...fields: Fields to containnegateMatch: Whether to add conditions negate match on target association
Parameters
-
string$associationPath Dot separated association path, 'Name1.Name2.Name3'.
-
Closure|null$builder optional the callback function to be used for setting extra options to the filtering query.
-
array<string, mixed>$options optional Extra options for the association matching.
Returns
$thisProperty Detail
$aliasList ¶ protected
Contains a list of the association names that are to be eagerly loaded.
Type
array<string, array<string, array<int, \Cake\ORM\EagerLoadable>>>$autoFields ¶ protected
Controls whether fields from associated tables will be eagerly loaded. When set to false, no fields will be loaded from associations.
Type
bool$containOptions ¶ protected
List of options accepted by associations in contain() index by key for faster access.
Type
array<string, int>$containments ¶ protected
Nested array describing the association to be fetched and the options to apply for each of them, if any
Type
array<string, array>$joinsMap ¶ protected
A map of table aliases pointing to the association objects they represent for the query.
Type
array<string, \Cake\ORM\EagerLoadable>$loadExternal ¶ protected
A list of associations that should be loaded with a separate query.
Type
array<int, \Cake\ORM\EagerLoadable>$matching ¶ protected
Another EagerLoader instance that will be used for 'matching' associations.
Type
Cake\ORM\EagerLoader|null$normalized ¶ protected
Contains a nested array with the compiled containments tree This is a normalized version of the user provided containments array.
Type
array<string, \Cake\ORM\EagerLoadable>|null