-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathLazyEagerLoader.php
175 lines (154 loc) · 6.51 KB
/
LazyEagerLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM;
use Cake\Collection\Collection;
use Cake\Collection\CollectionInterface;
use Cake\Database\Expression\TupleComparison;
use Cake\Datasource\EntityInterface;
/**
* Contains methods that are capable of injecting eagerly loaded associations into
* entities or lists of entities by using the same syntax as the EagerLoader.
*
* @internal
*/
class LazyEagerLoader
{
/**
* Loads the specified associations in the passed entity or list of entities
* by executing extra queries in the database and merging the results in the
* appropriate properties.
*
* The properties for the associations to be loaded will be overwritten on each entity.
*
* @param \Cake\Datasource\EntityInterface|\Cake\Datasource\EntityInterface[] $entities a single entity or list of entities
* @param array $contain A `contain()` compatible array.
* @see \Cake\ORM\Query::contain()
* @param \Cake\ORM\Table $source The table to use for fetching the top level entities
* @return \Cake\Datasource\EntityInterface|\Cake\Datasource\EntityInterface[]
*/
public function loadInto($entities, array $contain, Table $source)
{
$returnSingle = false;
if ($entities instanceof EntityInterface) {
$entities = [$entities];
$returnSingle = true;
}
$entities = new Collection($entities);
$query = $this->_getQuery($entities, $contain, $source);
$associations = array_keys($query->getContain());
$entities = $this->_injectResults($entities, $query, $associations, $source);
return $returnSingle ? array_shift($entities) : $entities;
}
/**
* Builds a query for loading the passed list of entity objects along with the
* associations specified in $contain.
*
* @param \Cake\Collection\CollectionInterface $objects The original entities
* @param array $contain The associations to be loaded
* @param \Cake\ORM\Table $source The table to use for fetching the top level entities
* @return \Cake\ORM\Query
*/
protected function _getQuery(CollectionInterface $objects, array $contain, Table $source): Query
{
$primaryKey = $source->getPrimaryKey();
$method = is_string($primaryKey) ? 'get' : 'extract';
$keys = $objects->map(function ($entity) use ($primaryKey, $method) {
return $entity->{$method}($primaryKey);
});
$query = $source
->find()
->select((array)$primaryKey)
->where(function ($exp, $q) use ($primaryKey, $keys, $source) {
/**
* @var \Cake\Database\Expression\QueryExpression $exp
* @var \Cake\ORM\Query $q
*/
if (is_array($primaryKey) && count($primaryKey) === 1) {
$primaryKey = current($primaryKey);
}
if (is_string($primaryKey)) {
return $exp->in($source->aliasField($primaryKey), $keys->toList());
}
$types = array_intersect_key($q->getDefaultTypes(), array_flip($primaryKey));
$primaryKey = array_map([$source, 'aliasField'], $primaryKey);
return new TupleComparison($primaryKey, $keys->toList(), $types, 'IN');
})
->enableAutoFields()
->contain($contain);
foreach ($query->getEagerLoader()->attachableAssociations($source) as $loadable) {
$config = $loadable->getConfig();
$config['includeFields'] = true;
$loadable->setConfig($config);
}
return $query;
}
/**
* Returns a map of property names where the association results should be injected
* in the top level entities.
*
* @param \Cake\ORM\Table $source The table having the top level associations
* @param string[] $associations The name of the top level associations
* @return string[]
*/
protected function _getPropertyMap(Table $source, array $associations): array
{
$map = [];
$container = $source->associations();
foreach ($associations as $assoc) {
/** @psalm-suppress PossiblyNullReference */
$map[$assoc] = $container->get($assoc)->getProperty();
}
return $map;
}
/**
* Injects the results of the eager loader query into the original list of
* entities.
*
* @param \Cake\Datasource\EntityInterface[]|\Traversable $objects The original list of entities
* @param \Cake\Collection\CollectionInterface|\Cake\ORM\Query $results The loaded results
* @param string[] $associations The top level associations that were loaded
* @param \Cake\ORM\Table $source The table where the entities came from
* @return array
*/
protected function _injectResults(iterable $objects, $results, array $associations, Table $source): array
{
$injected = [];
$properties = $this->_getPropertyMap($source, $associations);
$primaryKey = (array)$source->getPrimaryKey();
$results = $results
->indexBy(function ($e) use ($primaryKey) {
/** @var \Cake\Datasource\EntityInterface $e */
return implode(';', $e->extract($primaryKey));
})
->toArray();
foreach ($objects as $k => $object) {
$key = implode(';', $object->extract($primaryKey));
if (!isset($results[$key])) {
$injected[$k] = $object;
continue;
}
/** @var \Cake\Datasource\EntityInterface $loaded */
$loaded = $results[$key];
foreach ($associations as $assoc) {
$property = $properties[$assoc];
$object->set($property, $loaded->get($property), ['useSetters' => false]);
$object->setDirty($property, false);
}
$injected[$k] = $object;
}
return $injected;
}
}