Class SelectLoader
Implements the logic for loading an association using a SELECT query
Property Summary
-
$alias protected
stringThe alias of the association loading the results
-
$associationType protected
stringThe type of the association triggering the load
-
$bindingKey protected
array|stringThe binding key for the source association.
-
$finder protected
callableA callable that will return a query object used for loading the association results
-
$foreignKey protected
array|stringThe foreignKey to the target association
-
$sort protected
Cake\Database\ExpressionInterface|Closure|array|string|nullThe sorting options for loading the association
-
$sourceAlias protected
stringThe alias of the source association
-
$strategy protected
stringThe strategy to use for loading, either select or subquery
-
$targetAlias protected
stringThe alias of the target association
Method Summary
-
__construct() public
Copies the options array to properties in this class. The keys in the array correspond to properties in this class.
-
addFilteringCondition() protected
Appends any conditions required to load the relevant set of records in the target table query given a filter key and some filtering values.
-
addFilteringJoin() protected
Appends 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.
-
assertFieldsPresent() protected
Checks that the fetching query either has auto fields on or has the foreignKey fields selected. If the required fields are missing, automatically adds them to ensure entities can be properly identified and loaded.
-
buildEagerLoader() public
Returns a callable that can be used for injecting association results into a given iterator. The options accepted by this method are the same as
Association::eagerLoader() -
buildQuery() protected
Auxiliary 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() protected
Builds an array containing the results from fetchQuery indexed by the foreignKey value corresponding to this association.
-
buildSubquery() protected
Builds 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.
-
createTupleCondition() protected
Returns 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() protected
Returns the default options to use for the eagerLoader
-
extractFinder() protected
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']]]]]);
-
isAggregate() protected
Checks whether a column aggregates rows, in which case it must stay out of the GROUP BY.
-
linkField() protected
Generates a string used as a table field that contains the values upon which the filter should be applied
-
multiKeysInjector() protected
Returns 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
-
orderColumn() protected
Resolves the column an ORDER BY part sorts on, so it can join the subquery GROUP BY.
Associative parts (
['Articles.title' => 'ASC']) carry it as their key, orderByAsc()/ orderByDesc() and raw expressions in the value. Plain SQL fragments such asorderBy('Articles.title DESC')cannot be told apart from their direction. -
resultInjector() protected
Returns a callable to be used for each row in a query result set for injecting the eager loaded rows
-
rewriteJoinIdentifier() protected
Rewrites a subquery field reference for use in the outer join condition.
The subquery body must continue to reference its own internal table alias, while the outer join condition must reference the alias assigned to the derived table itself.
-
subqueryFields() protected
Calculate the fields that need to participate in a subquery.
Normally this includes the binding key columns. If the subquery keeps an ORDER BY, whatever it sorts on joins the GROUP BY too, and aliased columns additionally stay in the SELECT list as they may be calculated or constant values needed to load the correct association data.
When a HAVING clause is present the original SELECT aliases are preserved as well, since HAVING may reference computed aliases that would otherwise be dropped from the reduced subquery SELECT list.
Method Detail
__construct() ¶ public
__construct(array<string, mixed> $options)
Copies the options array to properties in this class. The keys in the array correspond to properties in this class.
Parameters
-
array<string, mixed>$options Properties to be copied to this class
addFilteringCondition() ¶ protected
addFilteringCondition(Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $query, array<string>|string $key, mixed $filter): Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>
Appends 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\SelectQuery<Cake\Datasource\EntityInterface|array>$query Target table's query
-
array<string>|string$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\SelectQuery<Cake\Datasource\EntityInterface|array>addFilteringJoin() ¶ protected
addFilteringJoin(Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $query, array<string>|string $key, Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $subquery): Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>
Appends 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\SelectQuery<Cake\Datasource\EntityInterface|array>$query Target table's query
-
array<string>|string$key the fields that should be used for filtering
-
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>$subquery The Subquery to use for filtering
Returns
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>assertFieldsPresent() ¶ protected
assertFieldsPresent(Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $fetchQuery, array<string> $key): void
Checks that the fetching query either has auto fields on or has the foreignKey fields selected. If the required fields are missing, automatically adds them to ensure entities can be properly identified and loaded.
Parameters
-
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>$fetchQuery The association fetching query
-
array<string>$key The foreign key fields to check
Returns
voidThrows
InvalidArgumentExceptionbuildEagerLoader() ¶ public
buildEagerLoader(array<string, mixed> $options): Closure
Returns a callable that can be used for injecting association results into a given
iterator. The options accepted by this method are the same as Association::eagerLoader()
Parameters
-
array<string, mixed>$options Same options as
Association::eagerLoader()
Returns
ClosurebuildQuery() ¶ protected
buildQuery(array<string, mixed> $options): Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>
Auxiliary 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<string, mixed>$options options accepted by eagerLoader()
Returns
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>Throws
InvalidArgumentExceptionWhen a key is required for associations but not selected.
buildResultMap() ¶ protected
buildResultMap(Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $fetchQuery, array<string, mixed> $options): array<string, mixed>
Builds an array containing the results from fetchQuery indexed by the foreignKey value corresponding to this association.
Parameters
-
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>$fetchQuery The query to get results from
-
array<string, mixed>$options The options passed to the eager loader
Returns
array<string, mixed>buildSubquery() ¶ protected
buildSubquery(Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $query): Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>
Builds 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\SelectQuery<Cake\Datasource\EntityInterface|array>$query the original query used to load source records
Returns
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>createTupleCondition() ¶ protected
createTupleCondition(Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $query, array<string> $keys, mixed $filter, string $operator): Cake\Database\Expression\TupleComparison
Returns 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\SelectQuery<Cake\Datasource\EntityInterface|array>$query Target table's query
-
array<string>$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\TupleComparisondefaultOptions() ¶ protected
defaultOptions(): array<string, mixed>
Returns the default options to use for the eagerLoader
Returns
array<string, mixed>extractFinder() ¶ protected
extractFinder(array|string $finderData): array
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
-
array|string$finderData The finder name or an array having the name as key and options as value.
Returns
arrayisAggregate() ¶ protected
isAggregate(mixed $column): bool
Checks whether a column aggregates rows, in which case it must stay out of the GROUP BY.
Parameters
-
mixed$column The column to check.
Returns
boollinkField() ¶ protected
linkField(array<string, mixed> $options): array<string>|string
Generates a string used as a table field that contains the values upon which the filter should be applied
Parameters
-
array<string, mixed>$options The options for getting the link field.
Returns
array<string>|stringThrows
Cake\Database\Exception\DatabaseExceptionmultiKeysInjector() ¶ protected
multiKeysInjector(array<string, mixed> $resultMap, array<string> $sourceKeys, string $nestKey): Closure
Returns 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<string, mixed>$resultMap A keyed arrays containing the target table
-
array<string>$sourceKeys An array with aliased keys to match
-
string$nestKey The key under which results should be nested
Returns
ClosureorderColumn() ¶ protected
orderColumn(mixed $field, mixed $direction): Cake\Database\ExpressionInterface|string|null
Resolves the column an ORDER BY part sorts on, so it can join the subquery GROUP BY.
Associative parts (['Articles.title' => 'ASC']) carry it as their key, orderByAsc()/
orderByDesc() and raw expressions in the value. Plain SQL fragments such as
orderBy('Articles.title DESC') cannot be told apart from their direction.
Parameters
-
mixed$field The key of the ORDER BY part.
-
mixed$direction The value of the ORDER BY part.
Returns
Cake\Database\ExpressionInterface|string|nullThe column, or null if it cannot be resolved.
resultInjector() ¶ protected
resultInjector(Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $fetchQuery, array<string, mixed> $resultMap, array<string, mixed> $options): Closure
Returns a callable to be used for each row in a query result set for injecting the eager loaded rows
Parameters
-
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>$fetchQuery the Query used to fetch results
-
array<string, mixed>$resultMap an array with the foreignKey as keys and the corresponding target table results as value.
-
array<string, mixed>$options The options passed to the eagerLoader method
Returns
ClosurerewriteJoinIdentifier() ¶ protected
rewriteJoinIdentifier(Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $query, mixed $field, string $aliasedTable): mixed
Rewrites a subquery field reference for use in the outer join condition.
The subquery body must continue to reference its own internal table alias, while the outer join condition must reference the alias assigned to the derived table itself.
Parameters
-
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>$query Target table's query
-
mixed$field The original selected field.
-
string$aliasedTable The alias assigned to the joined subquery.
Returns
mixedsubqueryFields() ¶ protected
subqueryFields(Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array> $query, bool $includeOrderFields = false): array<string, array>
Calculate the fields that need to participate in a subquery.
Normally this includes the binding key columns. If the subquery keeps an ORDER BY, whatever it sorts on joins the GROUP BY too, and aliased columns additionally stay in the SELECT list as they may be calculated or constant values needed to load the correct association data.
When a HAVING clause is present the original SELECT aliases are preserved as well, since HAVING may reference computed aliases that would otherwise be dropped from the reduced subquery SELECT list.
Parameters
-
Cake\ORM\Query\SelectQuery<Cake\Datasource\EntityInterface|array>$query The query to get fields from.
-
bool$includeOrderFields optional Whether ordered fields should be included in the SELECT list.
Returns
array<string, array>The list of fields for the subquery.