1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: VITALYIEGOROV |
5
|
|
|
* Date: 11.12.15 |
6
|
|
|
* Time: 17:35 |
7
|
|
|
*/ |
8
|
|
|
namespace samsoncms\api\query; |
9
|
|
|
|
10
|
|
|
use samsoncms\api\CMS; |
11
|
|
|
use samsoncms\api\exception\EntityFieldNotFound; |
12
|
|
|
use samsoncms\api\Field; |
13
|
|
|
use samsoncms\api\Material; |
14
|
|
|
use samsonframework\orm\ArgumentInterface; |
15
|
|
|
use samsonframework\orm\Condition; |
16
|
|
|
use samsonframework\orm\QueryInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Generic SamsonCMS Entity query. |
20
|
|
|
* @package samsoncms\api\query |
21
|
|
|
*/ |
22
|
|
|
class Entity extends Generic |
23
|
|
|
{ |
24
|
|
|
/** @var array Collection of all additional fields names */ |
25
|
|
|
protected static $fieldNames = array(); |
26
|
|
|
|
27
|
|
|
/** @var array Collection of localized additional fields identifiers */ |
28
|
|
|
protected static $localizedFieldIDs = array(); |
29
|
|
|
|
30
|
|
|
/** @var array Collection of NOT localized additional fields identifiers */ |
31
|
|
|
protected static $notLocalizedFieldIDs = array(); |
32
|
|
|
|
33
|
|
|
/** @var array Collection of all additional fields identifiers */ |
34
|
|
|
protected static $fieldIDs = array(); |
35
|
|
|
|
36
|
|
|
/** @var @var array Collection of additional fields value column names */ |
37
|
|
|
protected static $fieldValueColumns = array(); |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** @var array Collection of entity field filter */ |
41
|
|
|
protected $fieldFilter = array(); |
42
|
|
|
|
43
|
|
|
/** @var string Query locale */ |
44
|
|
|
protected $locale = ''; |
45
|
|
|
|
46
|
|
|
/** @var array Collection of ordering parameters */ |
47
|
|
|
protected $orderBy = array(); |
48
|
|
|
|
49
|
|
|
/** @var array Collection of limit parameters */ |
50
|
|
|
protected $limit = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Select specified entity fields. |
54
|
|
|
* If this method is called then only selected entity fields |
55
|
|
|
* would be return in entity instances. |
56
|
|
|
* |
57
|
|
|
* @param mixed $fieldNames Entity field name or collection of names |
58
|
|
|
* @return self Chaining |
59
|
|
|
*/ |
60
|
|
|
public function select($fieldNames) |
61
|
|
|
{ |
62
|
|
|
// Convert argument to array and iterate |
63
|
|
|
foreach ((!is_array($fieldNames) ? array($fieldNames) : $fieldNames) as $fieldName) { |
64
|
|
|
// Try to find entity additional field |
65
|
|
|
$pointer = &static::$fieldNames[$fieldName]; |
66
|
|
|
if (isset($pointer)) { |
67
|
|
|
// Store selected additional field buy FieldID and Field name |
68
|
|
|
$this->selectedFields[$pointer] = $fieldName; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set additional field for sorting. |
77
|
|
|
* |
78
|
|
|
* @param string $fieldName Additional field name |
79
|
|
|
* @param string $order Sorting order |
80
|
|
|
* @return self Chaining |
81
|
|
|
*/ |
82
|
|
|
public function orderBy($fieldName, $order = 'ASC') |
83
|
|
|
{ |
84
|
|
|
$this->orderBy = array($fieldName, $order); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set resulting query limits. |
91
|
|
|
* |
92
|
|
|
* @param integer $offset Starting index |
93
|
|
|
* @param integer|null $count Entities count |
94
|
|
|
* @return self Chaining |
95
|
|
|
*/ |
96
|
|
|
public function limit($offset, $count = null) |
97
|
|
|
{ |
98
|
|
|
$this->limit = array($offset, $count); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Add condition to current query. |
105
|
|
|
* |
106
|
|
|
* @param string $fieldName Entity field name |
107
|
|
|
* @param string $fieldValue Value |
108
|
|
|
* @return self Chaining |
109
|
|
|
*/ |
110
|
|
|
public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL) |
111
|
|
|
{ |
112
|
|
|
// Try to find entity additional field |
113
|
|
|
$pointer = &static::$fieldNames[$fieldName]; |
114
|
|
|
if (isset($pointer)) { |
115
|
|
|
// Store additional field filter value |
116
|
|
|
$this->fieldFilter[$pointer] = $fieldValue; |
117
|
|
|
} else { |
118
|
|
|
parent::where($fieldName, $fieldValue, $fieldRelation); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** @return array Collection of entity identifiers */ |
125
|
|
|
protected function findEntityIDs() |
126
|
|
|
{ |
127
|
|
|
$entityIDs = array(); |
128
|
|
|
if ($this->conditions) { |
129
|
|
|
$entityIDs = $this->query |
130
|
|
|
->entity(Material::ENTITY) |
131
|
|
|
->whereCondition($this->conditions) |
132
|
|
|
->fields(Material::F_PRIMARY); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// TODO: Find and describe approach with maximum generic performance |
136
|
|
|
$entityIDs = $this->findByAdditionalFields( |
137
|
|
|
$this->fieldFilter, |
138
|
|
|
$this->findByNavigationIDs($entityIDs) |
|
|
|
|
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
// Perform sorting if necessary |
142
|
|
|
if (sizeof($this->orderBy) == 2) { |
143
|
|
|
$entityIDs = $this->applySorting($entityIDs, $this->orderBy[0], $this->orderBy[1]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// Perform limits if necessary |
147
|
|
|
if (sizeof($this->limit)) { |
148
|
|
|
$entityIDs = array_slice($entityIDs, $this->limit[0], $this->limit[1]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $entityIDs; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get collection of entity identifiers filtered by navigation identifiers. |
156
|
|
|
* |
157
|
|
|
* @param array $entityIDs Additional collection of entity identifiers for filtering |
158
|
|
|
* @return array Collection of material identifiers by navigation identifiers |
159
|
|
|
*/ |
160
|
|
|
protected function findByNavigationIDs($entityIDs = array()) |
161
|
|
|
{ |
162
|
|
|
return (new MaterialNavigation($entityIDs))->idsByRelationID(static::$navigationIDs); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get collection of entity identifiers filtered by additional field and its value. |
167
|
|
|
* |
168
|
|
|
* @param array $additionalFields Collection of additional field identifiers => values |
169
|
|
|
* @param array $entityIDs Additional collection of entity identifiers for filtering |
170
|
|
|
* @return array Collection of material identifiers by navigation identifiers |
171
|
|
|
*/ |
172
|
|
|
protected function findByAdditionalFields($additionalFields, $entityIDs = array()) |
173
|
|
|
{ |
174
|
|
|
/** |
175
|
|
|
* TODO: We have separate request to materialfield for each field, maybe faster to |
176
|
|
|
* make one single query with all fields conditions. Performance tests are needed. |
177
|
|
|
*/ |
178
|
|
|
|
179
|
|
|
// Iterate all additional fields needed for filter entity |
180
|
|
|
foreach ($additionalFields as $fieldID => $fieldValue) { |
181
|
|
|
// Get collection of entity identifiers passing already found identifiers |
182
|
|
|
$entityIDs = (new MaterialField($entityIDs))->idsByRelationID($fieldID, $fieldValue); |
183
|
|
|
|
184
|
|
|
// Stop execution if we have no entities found at this step |
185
|
|
|
if (!sizeof($entityIDs)) { |
186
|
|
|
break; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $entityIDs; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Add sorting to entity identifiers. |
195
|
|
|
* |
196
|
|
|
* @param array $entityIDs |
197
|
|
|
* @param string $fieldName Additional field name for sorting |
198
|
|
|
* @param string $order Sorting order(ASC|DESC) |
199
|
|
|
* @return array Collection of entity identifiers ordered by additional field value |
200
|
|
|
*/ |
201
|
|
|
protected function applySorting(array $entityIDs, $fieldName, $order = 'ASC') |
202
|
|
|
{ |
203
|
|
|
// Get additional field metadata |
204
|
|
|
$fieldID = static::$fieldNames[$fieldName]; |
205
|
|
|
$valueColumn = static::$fieldValueColumns[$fieldID]; |
206
|
|
|
|
207
|
|
|
return $this->query |
208
|
|
|
->entity(CMS::MATERIAL_FIELD_RELATION_ENTITY) |
209
|
|
|
->where(Field::F_PRIMARY, $fieldID) |
210
|
|
|
->where(Material::F_PRIMARY, $entityIDs) |
|
|
|
|
211
|
|
|
->orderBy($valueColumn, $order) |
212
|
|
|
->fields(Material::F_PRIMARY); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get entities additional field values. |
217
|
|
|
* |
218
|
|
|
* @param array $entityIDs Collection of entity identifiers |
219
|
|
|
* @return array Collection of entities additional fields EntityID => [Additional field name => Value] |
220
|
|
|
*/ |
221
|
|
|
protected function findAdditionalFields($entityIDs) |
222
|
|
|
{ |
223
|
|
|
$return = array(); |
224
|
|
|
|
225
|
|
|
// Copy fields arrays |
226
|
|
|
$localized = static::$localizedFieldIDs; |
227
|
|
|
$notLocalized = static::$notLocalizedFieldIDs; |
228
|
|
|
|
229
|
|
|
// If we filter additional fields that we need to receive |
230
|
|
|
if (sizeof($this->selectedFields)) { |
231
|
|
|
foreach ($this->selectedFields as $fieldID => $fieldName) { |
232
|
|
|
// Filter localized and not fields by selected fields |
233
|
|
|
if (!isset(static::$localizedFieldIDs[$fieldID])) { |
234
|
|
|
unset($localized[$fieldID]); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (!isset(static::$notLocalizedFieldIDs[$fieldID])) { |
238
|
|
|
unset($notLocalized[$fieldID]); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// Prepare localized additional field query condition |
244
|
|
|
$condition = new Condition(Condition::DISJUNCTION); |
245
|
|
|
foreach ($localized as $fieldID => $fieldName) { |
246
|
|
|
$condition->addCondition( |
247
|
|
|
(new Condition()) |
248
|
|
|
->add(Field::F_PRIMARY, $fieldID) |
249
|
|
|
->add(Field::F_LOCALIZED, $this->locale) |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
// Prepare not localized fields condition |
254
|
|
|
foreach ($notLocalized as $fieldID => $fieldName) { |
255
|
|
|
$condition->add(Field::F_PRIMARY, $fieldID); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
// Get additional fields values for current entity identifiers |
259
|
|
|
foreach ($this->query->entity(CMS::MATERIAL_FIELD_RELATION_ENTITY) |
|
|
|
|
260
|
|
|
->where(Material::F_PRIMARY, $entityIDs) |
|
|
|
|
261
|
|
|
->whereCondition($condition) |
262
|
|
|
->where(Material::F_DELETION, true) |
|
|
|
|
263
|
|
|
->exec() as $additionalField |
264
|
|
|
) { |
265
|
|
|
// Get needed metadata |
266
|
|
|
$fieldID = $additionalField[Field::F_PRIMARY]; |
267
|
|
|
$materialID = $additionalField[Material::F_PRIMARY]; |
268
|
|
|
$valueField = static::$fieldValueColumns[$fieldID]; |
269
|
|
|
$fieldName = static::$fieldIDs[$fieldID]; |
270
|
|
|
$fieldValue = $additionalField[$valueField]; |
271
|
|
|
|
272
|
|
|
// Gather additional fields values by entity identifiers and field name |
273
|
|
|
$return[$materialID][$fieldName] = $fieldValue; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $return; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Fill entity additional fields. |
281
|
|
|
* |
282
|
|
|
* @param Entity $entity Entity instance for filling |
283
|
|
|
* @param array $additionalFields Collection of additional field values |
284
|
|
|
* @return Entity With filled additional field values |
285
|
|
|
*/ |
286
|
|
|
protected function fillEntityFields($entity, array $additionalFields) |
287
|
|
|
{ |
288
|
|
|
// If we have list of additional fields that we need |
289
|
|
|
$fieldIDs = sizeof($this->selectedFields) ? $this->selectedFields : static::$fieldIDs; |
290
|
|
|
|
291
|
|
|
// Iterate all entity additional fields |
292
|
|
|
foreach ($fieldIDs as $variable) { |
293
|
|
|
// Set only existing additional fields |
294
|
|
|
$pointer = &$additionalFields[$entity[Material::F_PRIMARY]][$variable]; |
295
|
|
|
if (isset($pointer)) { |
296
|
|
|
$entity->$variable = $pointer; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return $entity; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Perform SamsonCMS query and get collection of entities. |
305
|
|
|
* |
306
|
|
|
* @return \samsoncms\api\Entity[] Collection of entity fields |
307
|
|
|
*/ |
308
|
|
|
public function find() |
309
|
|
|
{ |
310
|
|
|
$return = array(); |
311
|
|
|
if (sizeof($entityIDs = $this->findEntityIDs())) { |
312
|
|
|
$additionalFields = $this->findAdditionalFields($entityIDs); |
313
|
|
|
|
314
|
|
|
// Set entity primary keys |
315
|
|
|
$this->primary($entityIDs); |
316
|
|
|
|
317
|
|
|
//elapsed('End fields values'); |
318
|
|
|
/** @var \samsoncms\api\Entity $item Find entity instances */ |
319
|
|
|
foreach (parent::find() as $item) { |
|
|
|
|
320
|
|
|
$item = $this->fillEntityFields($item, $additionalFields); |
321
|
|
|
|
322
|
|
|
// Store entity by identifier |
323
|
|
|
$return[$item[Material::F_PRIMARY]] = $item; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
//elapsed('Finish SamsonCMS '.static::$identifier.' query'); |
328
|
|
|
|
329
|
|
|
return $return; |
|
|
|
|
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Perform SamsonCMS query and get first matching entity. |
334
|
|
|
* |
335
|
|
|
* @return \samsoncms\api\Entity Firt matching entity |
336
|
|
|
*/ |
337
|
|
|
public function first() |
338
|
|
|
{ |
339
|
|
|
$return = array(); |
340
|
|
|
if (sizeof($entityIDs = $this->findEntityIDs())) { |
341
|
|
|
$this->primary($entityIDs); |
342
|
|
|
$additionalFields = $this->findAdditionalFields($entityIDs); |
343
|
|
|
$return = $this->fillEntityFields(parent::first(), $additionalFields); |
|
|
|
|
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
return $return; |
|
|
|
|
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Perform SamsonCMS query and get collection of entities fields. |
351
|
|
|
* |
352
|
|
|
* @param string $fieldName Entity field name |
353
|
|
|
* @return array Collection of entity fields |
354
|
|
|
* @throws EntityFieldNotFound |
355
|
|
|
*/ |
356
|
|
|
public function fields($fieldName) |
357
|
|
|
{ |
358
|
|
|
$return = array(); |
|
|
|
|
359
|
|
|
if (sizeof($entityIDs = $this->findEntityIDs())) { |
360
|
|
|
// Check if our entity has this field |
361
|
|
|
$fieldID = &static::$fieldNames[$fieldName]; |
362
|
|
|
if (isset($fieldID)) { |
363
|
|
|
$return = $this->query |
364
|
|
|
->entity(\samsoncms\api\MaterialField::ENTITY) |
365
|
|
|
->where(Material::F_PRIMARY, $entityIDs) |
366
|
|
|
->where(Field::F_PRIMARY, $fieldID) |
367
|
|
|
->where(\samsoncms\api\MaterialField::F_DELETION, true) |
|
|
|
|
368
|
|
|
->fields(static::$fieldValueColumns[$fieldID]); |
369
|
|
|
} else { |
370
|
|
|
throw new EntityFieldNotFound($fieldName); |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
//elapsed('Finish SamsonCMS '.static::$identifier.' query'); |
375
|
|
|
|
376
|
|
|
return $return; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Generic constructor. |
381
|
|
|
* |
382
|
|
|
* @param QueryInterface $query Database query instance |
383
|
|
|
* @param string $locale Query localizaation |
384
|
|
|
*/ |
385
|
|
|
public function __construct(QueryInterface $query, $locale = NULL) |
|
|
|
|
386
|
|
|
{ |
387
|
|
|
$this->locale = $locale; |
388
|
|
|
|
389
|
|
|
parent::__construct($query); |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: