1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: VITALYIEGOROV |
5
|
|
|
* Date: 08.12.15 |
6
|
|
|
* Time: 23:11 |
7
|
|
|
*/ |
8
|
|
|
namespace samsoncms\api\query; |
9
|
|
|
|
10
|
|
|
use samson\activerecord\dbQuery; |
11
|
|
|
use samsoncms\api\Material; |
|
|
|
|
12
|
|
|
use samsoncms\api\MaterialField; |
|
|
|
|
13
|
|
|
use samsonframework\orm\Query; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Material with additional fields query. |
17
|
|
|
* @package samsoncms\api |
18
|
|
|
*/ |
19
|
|
|
class Generic |
20
|
|
|
{ |
21
|
|
|
/** @var string Entity identifier */ |
22
|
|
|
protected static $identifier; |
23
|
|
|
|
24
|
|
|
/** @var string Entity navigation identifiers */ |
25
|
|
|
protected static $navigationIDs = 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 entity field filter */ |
34
|
|
|
protected $fieldFilter = array(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add condition to current query. |
38
|
|
|
* |
39
|
|
|
* @param string $fieldName Entity field name |
40
|
|
|
* @param string $fieldValue Value |
41
|
|
|
* @return self Chaining |
42
|
|
|
*/ |
43
|
|
|
public function where($fieldName, $fieldValue = null) |
44
|
|
|
{ |
45
|
|
|
// Try to find entity additional field |
46
|
|
|
if (property_exists(static::$identifier, $fieldName)) { |
47
|
|
|
// Store additional field filter value |
48
|
|
|
$this->fieldFilter[$fieldName] = $fieldValue; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get collection of entity identifiers filtered by navigation identifiers. |
56
|
|
|
* |
57
|
|
|
* @param array $entityIDs Additional collection of entity identifiers for filtering |
58
|
|
|
* @return array Collection of material identifiers by navigation identifiers |
59
|
|
|
*/ |
60
|
|
|
protected function findByNavigationIDs($entityIDs = array()) |
61
|
|
|
{ |
62
|
|
|
return (new MaterialNavigation($entityIDs))->idsByRelationID(static::$navigationIDs); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get collection of entity identifiers filtered by additional field and its value. |
67
|
|
|
* |
68
|
|
|
* @param array $additionalFields Collection of additional field identifiers => values |
69
|
|
|
* @param array $entityIDs Additional collection of entity identifiers for filtering |
70
|
|
|
* @return array Collection of material identifiers by navigation identifiers |
71
|
|
|
*/ |
72
|
|
|
protected function findByAdditionalFields($additionalFields, $entityIDs = array()) |
73
|
|
|
{ |
74
|
|
|
// Iterate all additional fields needed for filter entity |
75
|
|
|
foreach ($additionalFields as $fieldID => $fieldValue) { |
76
|
|
|
// Get collection of entity identifiers passing already found identifiers |
77
|
|
|
$entityIDs = (new MaterialField($entityIDs))->idsByRelationID($fieldID, $fieldValue); |
|
|
|
|
78
|
|
|
|
79
|
|
|
// Stop execution if we have no entities found at this step |
80
|
|
|
if (!sizeof($entityIDs)) { |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $entityIDs; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function findAdditionalFields($entityIDs) |
89
|
|
|
{ |
90
|
|
|
$return = array(); |
91
|
|
|
foreach (MaterialField::byFieldIDAndMaterialID(new dbQuery(), array_values(static::$fieldIDs), $entityIDs) as $additionalField) { |
|
|
|
|
92
|
|
|
$return[$additionalField[Material::F_PRIMARY]] = $additionalField; |
93
|
|
|
} |
94
|
|
|
return $return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Perform SamsonCMS query and get entities collection. |
99
|
|
|
* |
100
|
|
|
* @return mixed[] Collection of found entities |
101
|
|
|
*/ |
102
|
|
|
public function find() |
103
|
|
|
{ |
104
|
|
|
// TODO: Find and describe approach with maximum generic performance |
105
|
|
|
$entityIDs = $this->findByNavigationIDs(); |
106
|
|
|
$entityIDs = $this->findByAdditionalFields($this->fieldFilter, $entityIDs); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$return = array(); |
109
|
|
|
if (sizeof($entityIDs)) { |
110
|
|
|
$additionalFields = $this->findAdditionalFields($entityIDs); |
111
|
|
|
/** @var \samsoncms\api\Entity $item Find entity instances */ |
112
|
|
|
foreach ((new \samsoncms\api\query\Material(static::$identifier))->byIDs($entityIDs, 'exec') as $item) { |
113
|
|
|
// Iterate all entity additional fields |
114
|
|
|
foreach (get_class_vars(static::$identifier) as $variable) { |
115
|
|
|
$item->$variable = &$additionalFields[$variable]; |
116
|
|
|
} |
117
|
|
|
// Store entity by identifier |
118
|
|
|
$return[$item[Material::F_PRIMARY]] = $item; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $return; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: