-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSelect.php
More file actions
255 lines (207 loc) · 7.11 KB
/
Select.php
File metadata and controls
255 lines (207 loc) · 7.11 KB
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
252
253
254
255
<?php
namespace UniMapper\Query;
use UniMapper\Entity\Reflection;
use UniMapper\Convention;
use UniMapper\Cache\ICache;
use UniMapper\Association;
class Select extends \UniMapper\Query
{
use Filterable;
use Limit;
use Selectable;
use Sortable;
const ASC = "asc",
DESC = "desc";
protected $cached = false;
protected $cachedOptions = [];
public function __construct(Reflection $reflection)
{
parent::__construct($reflection);
$this->select(array_slice(func_get_args(), 3));
}
public function cached($enable = true, array $options = [])
{
$this->cached = (bool) $enable;
$this->cachedOptions = $options;
return $this;
}
protected function onExecute(\UniMapper\Connection $connection)
{
$adapter = $connection->getAdapter($this->reflection->getAdapterName());
$mapper = $connection->getMapper();
$cache = null;
if ($this->cached) {
$cache = $connection->getCache();
}
if ($cache) {
$cachedResult = $cache->load($this->_getQueryChecksum());
if ($cachedResult) {
return $mapper->mapCollection(
$this->reflection->getName(),
$cachedResult
);
}
}
$query = $adapter->createSelect(
$this->reflection->getAdapterResource(),
$this->createSelection(),
$this->orderBy,
$this->limit,
$this->offset
);
if ($this->filter) {
$query->setFilter(
$mapper->unmapFilter(
$this->reflection,
$this->filter
)
);
}
if ($this->adapterAssociations) {
$query->setAssociations($this->adapterAssociations);
}
// Execute adapter query
$result = $adapter->execute($query);
// Get remote associations
if ($this->remoteAssociations && !empty($result)) {
settype($result, "array");
foreach ($this->remoteAssociations as $colName => $association) {
$assocKey = $association->getKey();
$assocValues = [];
foreach ($result as $item) {
if (is_array($item)) {
$assocValues[] = $item[$assocKey];
} else {
$assocValues[] = $item->{$assocKey};
}
}
$associated = $association->load($connection, $assocValues);
// Merge returned associations
if (!empty($associated)) {
$result = $this->_mergeAssociated(
$result,
$associated,
$assocKey,
$colName
);
}
}
}
if ($cache) {
$cachedOptions = $this->cachedOptions;
// Cache invalidation should depend on entity changes
if (isset($cachedOptions[ICache::FILES])) {
$cachedOptions[ICache::FILES] += $this->reflection->getRelatedFiles();
} else {
$cachedOptions[ICache::FILES] = $this->reflection->getRelatedFiles();
}
$cache->save(
$this->_getQueryChecksum(),
$result,
$cachedOptions
);
}
return $mapper->mapCollection(
$this->reflection->getName(),
empty($result) ? [] : $result
);
}
/**
* Merge associated data with result
*
* @param array $result
* @param array $associated
* @param string $refKey
* @param string $colName
*
* @return array
*/
private function _mergeAssociated(
array $result,
array $associated,
$refKey,
$colName
) {
foreach ($result as $index => $item) {
if (is_array($item)) {
$refValue = $item[$refKey];
} else {
$refValue = $item->{$refKey};
}
if (isset($associated[$refValue])) {
if (is_array($result[$index])) {
$result[$index][$colName] = $associated[$refValue];
} else {
$result[$index]->{$colName} = $associated[$refValue];
}
}
}
return $result;
}
/**
* Get a unique query checksum
*
* @return integer
*/
private function _getQueryChecksum()
{
return md5(
serialize(
[
"name" => $this->getName(),
"entity" => Convention::classToName(
$this->reflection->getClassName(), Convention::ENTITY_MASK
),
"limit" => $this->limit,
"offset" => $this->offset,
"selection" => $this->selection,
"orderBy" => $this->orderBy,
"adapterAssociations" => array_keys($this->adapterAssociations),
"remoteAssociations" => array_keys($this->remoteAssociations),
"conditions" => $this->filter
]
)
);
}
protected function createSelection()
{
$selection = [];
if (empty($this->selection)) {
foreach ($this->reflection->getProperties() as $property) {
// Exclude associations & computed properties & disabled mapping
if (!$property->hasOption(Reflection\Property\Option\Assoc::KEY)
&& !$property->hasOption(Reflection\Property\Option\Computed::KEY)
&& !($property->hasOption(Reflection\Property\Option\Map::KEY)
&& !$property->getOption(Reflection\Property\Option\Map::KEY))
) {
$selection[] = $property->getUnmapped();
}
}
} else {
// Add properties from filter
$selection = $this->selection;
// Include primary automatically if not provided
if ($this->reflection->hasPrimary()) {
$primaryName = $this->reflection
->getPrimaryProperty()
->getName();
if (!in_array($primaryName, $selection, true)) {
$selection[] = $primaryName;
}
}
// Unmap all names
foreach ($selection as $index => $name) {
$selection[$index] = $this->reflection->getProperty($name)->getUnmapped();
}
}
// Add required keys from remote associations
foreach ($this->remoteAssociations as $association) {
if (($association instanceof Association\ManyToOne || $association instanceof Association\OneToOne)
&& !in_array($association->getKey(), $selection, true)
) {
$selection[] = $association->getKey();
}
}
return $selection;
}
}