-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Query.php
251 lines (207 loc) · 6.1 KB
/
Query.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace Cake\ORM;
use Cake\Database\Query as DatabaseQuery;
use Cake\Database\Statement\BufferedStatement;
use Cake\Database\Statement\CallbackStatement;
use Cake\Utility\Hash;
class Query extends DatabaseQuery {
/**
* Instance of a table object this query is bound to
*
* @var \Cake\ORM\Table
*/
protected $_table;
protected $_containments;
protected $_hasFields;
protected $_aliasMap = [];
protected $_eagerLoading = false;
protected $_loadEagerly = [];
public function repository(Table $table = null) {
if ($table === null) {
return $this->_table;
}
$this->_table = $table;
$this->addDefaultTypes($table);
return $this;
}
public function addDefaultTypes(Table $table) {
$alias = $table->alias();
$fields = [];
foreach ($table->schema() as $f => $meta) {
$fields[$f] = $fields[$alias . '.' . $f] = $meta['type'];
}
$this->defaultTypes($this->defaultTypes() + $fields);
}
public function contain($associations = null) {
if ($this->_containments === null) {
$this->_containments = new \ArrayObject;
}
if ($associations === null) {
return $this->_containments;
}
foreach ((array)$associations as $table => $options) {
if (is_string($options)) {
$table = $options;
$options = [];
}
$this->_containments[$table] = $options;
}
return $this;
}
public function execute() {
return new ResultSet($this, parent::execute());
}
public function toArray() {
return $this->execute()->toArray();
}
public function aliasedTable($alias) {
return $this->_aliasMap[$alias];
}
public function aliasField($field, $alias = null) {
$namespaced = strpos($field, '.') !== false;
$_field = $field;
if ($namespaced) {
list($alias, $field) = explode('.', $field);
}
if (!$alias) {
$alias = $this->repository()->alias();
}
$key = sprintf('%s__%s', $alias, $field);
if (!$namespaced) {
$_field = $alias . '.' . $field;
}
return [$key => $_field];
}
public function aliasFields($fields, $defaultAlias = null) {
$aliased = [];
foreach ($fields as $alias => $field) {
if (is_numeric($alias) && is_string($field)) {
$aliased += $this->aliasField($field, $defaultAlias);
continue;
}
$aliased[$alias] = $field;
}
return $aliased;
}
/**
* Auxiliary function used to wrap the original statement from the driver with
* any registered callbacks. This will also setup the correct statement class
* in order to eager load deep associations.
*
* @param Cake\Database\Statement $statement to be decorated
* @return Cake\Database\Statement
*/
protected function _decorateResults($statement) {
$statement = parent::_decorateResults($statement);
if ($this->_eagerLoading) {
if (!($statement instanceof BufferedStatement)) {
$statement = new BufferedStatement($statement, $this->connection()->driver());
}
$statement = $this->_eagerLoad($statement);
}
return $statement;
}
protected function _transformQuery() {
if (!$this->_dirty) {
return parent::_transformQuery();
}
$this->from([$this->_table->alias() => $this->_table->table()]);
$this->_aliasMap[$this->_table->alias()] = $this->_table;
$this->_addDefaultFields();
$this->_addContainments();
return parent::_transformQuery();
}
protected function _addContainments() {
$this->_eagerLoading = false;
$this->_loadEagerly = [];
if (empty($this->_containments)) {
return;
}
$contain = [];
foreach ($this->_containments as $table => $options) {
$contain[$table] = $this->_normalizeContain(
$this->_table,
$table,
$options
);
}
$firstLevelJoins = $this->_resolveFirstLevel($this->_table, $contain);
foreach ($firstLevelJoins as $options) {
$this->_addJoin($options['association'], $options['options']);
}
foreach ($contain as $relation => $meta) {
if ($meta['instance'] && !$meta['instance']->canBeJoined()) {
$this->_eagerLoading = true;
$this->_loadEagerly[$relation] = $meta;
}
}
}
protected function _normalizeContain(Table $parent, $alias, $options) {
$defaults = [
'associations' => 1,
'foreignKey' => 1,
'conditions' => 1,
'fields' => 1
];
$instance = $parent->association($alias);
$table = $instance->target();
$this->_aliasMap[$alias] = $table;
$extra = array_diff_key($options, $defaults);
$config = [
'associations' => [],
'instance' => $instance,
'config' => array_diff_key($options, $extra)
];
foreach ($extra as $t => $assoc) {
if (is_numeric($t)) {
$t = $assoc;
$assoc = [];
}
$config['associations'][$t] = $this->_normalizeContain($table, $t, $assoc);
}
return $config;
}
protected function _resolveFirstLevel($source, $associations) {
$result = [];
foreach ($associations as $table => $options) {
$associated = $options['instance'];
if ($associated && $associated->canBeJoined()) {
$result[$table] = [
'association' => $associated,
'options' => $options['config']
];
$result += $this->_resolveFirstLevel($associated->target(), $options['associations']);
}
//TODO: If it is not associated assume a HasOne association (like in the popular Linkable plugin)
}
return $result;
}
protected function _addJoin($association, $options) {
$association->attachTo($this, $options + ['includeFields' => !$this->_hasFields]);
}
protected function _eagerLoad($statement) {
$keys = [];
$alias = $this->_table->alias();
$pkField = key($this->aliasField($this->_table->primaryKey()));
while($result = $statement->fetch('assoc')) {
$keys[$alias][] = $result[$pkField];
}
$statement->rewind();
foreach ($this->_loadEagerly as $association => $meta) {
$f = $meta['instance']->eagerLoader($keys[$alias], $meta);
$statement = new CallbackStatement($statement, $this->connection()->driver(), $f);
}
return $statement;
}
protected function _addDefaultFields() {
$select = $this->clause('select');
$this->_hasFields = true;
if (!count($select)) {
$this->_hasFields = false;
$this->select(array_keys($this->repository()->schema()));
$select = $this->clause('select');
}
$aliased = $this->aliasFields($select, $this->repository()->alias());
$this->select($aliased, true);
}
}