|
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 samsoncms\api\Material; |
|
11
|
|
|
use samsonframework\orm\ArgumentInterface; |
|
12
|
|
|
use samsonframework\orm\Condition; |
|
13
|
|
|
use samsonframework\orm\QueryInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Material with additional fields query. |
|
17
|
|
|
* @package samsoncms\api |
|
18
|
|
|
*/ |
|
19
|
|
|
class Generic |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var array Collection of all supported entity fields */ |
|
22
|
|
|
protected static $parentFields = array( |
|
23
|
|
|
Material::F_PRIORITY => Material::F_PRIORITY, |
|
24
|
|
|
Material::F_IDENTIFIER => Material::F_IDENTIFIER, |
|
25
|
|
|
Material::F_DELETION => Material::F_DELETION, |
|
26
|
|
|
Material::F_PUBLISHED => Material::F_PUBLISHED, |
|
27
|
|
|
Material::F_PARENT => Material::F_PARENT, |
|
28
|
|
|
Material::F_CREATED => Material::F_CREATED, |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
/** @var string Entity identifier */ |
|
32
|
|
|
protected static $identifier; |
|
33
|
|
|
|
|
34
|
|
|
/** @var string Entity navigation identifiers */ |
|
35
|
|
|
protected static $navigationIDs = array(); |
|
36
|
|
|
|
|
37
|
|
|
/** @var QueryInterface Database query instance */ |
|
38
|
|
|
protected $query; |
|
39
|
|
|
|
|
40
|
|
|
/** @var array Collection of entity fields to retrieved from database */ |
|
41
|
|
|
protected $selectedFields; |
|
42
|
|
|
|
|
43
|
|
|
/** @var Condition Query conditions */ |
|
44
|
|
|
protected $conditions; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Convert date value to database format. |
|
48
|
|
|
* TODO: Must implement at database layer |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $date Date value for conversion |
|
51
|
|
|
* @return string Converted date to correct format |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function convertToDateTime($date) |
|
54
|
|
|
{ |
|
55
|
|
|
return date("Y-m-d H:i:s", strtotime($date)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Add condition to current query. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $fieldName Entity field name |
|
62
|
|
|
* @param string $fieldValue Value |
|
63
|
|
|
* @return self Chaining |
|
64
|
|
|
*/ |
|
65
|
|
|
public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->conditions->add($fieldName, $fieldValue, $fieldRelation); |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Add primary field query condition. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $value Field value |
|
76
|
|
|
* @return self Chaining |
|
77
|
|
|
* @see Material::where() |
|
78
|
|
|
*/ |
|
79
|
|
|
public function primary($value) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->where(Material::F_PRIMARY, $value); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Add identifier field query condition. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $value Field value |
|
88
|
|
|
* @return self Chaining |
|
89
|
|
|
* @see Material::where() |
|
90
|
|
|
*/ |
|
91
|
|
|
public function identifier($value) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->where(Material::F_IDENTIFIER, $value); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Add active flag condition. |
|
98
|
|
|
* |
|
99
|
|
|
* @param bool $value Field value |
|
100
|
|
|
* @return self Chaining |
|
101
|
|
|
* @see Material::where() |
|
102
|
|
|
*/ |
|
103
|
|
|
public function active($value) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->where(Material::F_DELETION, $value); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Add entity published field query condition. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $value Field value |
|
112
|
|
|
* @return self Chaining |
|
113
|
|
|
* @see Material::where() |
|
114
|
|
|
*/ |
|
115
|
|
|
public function published($value) |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->where(Material::F_PUBLISHED, $value); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Add entity creation field query condition. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $value Field value |
|
124
|
|
|
* @param string $relation @see ArgumentInterface types |
|
125
|
|
|
* @return self Chaining |
|
126
|
|
|
* @see Material::where() |
|
127
|
|
|
*/ |
|
128
|
|
|
public function created($value, $relation = ArgumentInterface::EQUAL) |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->where(Material::F_CREATED, $this->convertToDateTime($value), $relation); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Add entity modification field query condition. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $value Field value |
|
137
|
|
|
* @param string $relation @see ArgumentInterface types |
|
138
|
|
|
* @return self Chaining |
|
139
|
|
|
* @see Material::where() |
|
140
|
|
|
*/ |
|
141
|
|
|
public function modified($value, $relation = ArgumentInterface::EQUAL) |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->where(Material::F_MODIFIED, $this->convertToDateTime($value), $relation); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Perform SamsonCMS query and get entities collection. |
|
148
|
|
|
* |
|
149
|
|
|
* @return \samsoncms\api\Entity[] Collection of found entities |
|
150
|
|
|
*/ |
|
151
|
|
|
public function find() |
|
152
|
|
|
{ |
|
153
|
|
|
// Proxy to regular database query |
|
154
|
|
|
return $this->query |
|
155
|
|
|
->entity(static::$identifier) |
|
156
|
|
|
->whereCondition($this->conditions) |
|
157
|
|
|
->exec(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Perform SamsonCMS query and get collection of entities fields. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $fieldName Entity field name |
|
164
|
|
|
* @return array Collection of entity fields |
|
165
|
|
|
*/ |
|
166
|
|
|
public function fields($fieldName) |
|
167
|
|
|
{ |
|
168
|
|
|
// Proxy to regular database query |
|
169
|
|
|
return $this->query |
|
170
|
|
|
->entity(static::$identifier) |
|
171
|
|
|
->whereCondition($this->conditions) |
|
172
|
|
|
->fields($fieldName); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Perform SamsonCMS query and get first matching entity. |
|
177
|
|
|
* |
|
178
|
|
|
* @return \samsoncms\api\Entity First matching entity |
|
179
|
|
|
*/ |
|
180
|
|
|
public function first() |
|
181
|
|
|
{ |
|
182
|
|
|
// Proxy to regular database query |
|
183
|
|
|
$return = $this->query |
|
184
|
|
|
->entity(static::$identifier) |
|
185
|
|
|
->limit(1) |
|
186
|
|
|
->whereCondition($this->conditions) |
|
187
|
|
|
->exec(); |
|
188
|
|
|
|
|
189
|
|
|
return array_shift($return); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Perform SamsonCMS query and get amount resulting entities. |
|
194
|
|
|
* |
|
195
|
|
|
* @return int Amount of resulting entities |
|
196
|
|
|
*/ |
|
197
|
|
|
public function count() |
|
198
|
|
|
{ |
|
199
|
|
|
// Proxy to regular database query |
|
200
|
|
|
return $this->query |
|
|
|
|
|
|
201
|
|
|
->entity(static::$identifier) |
|
202
|
|
|
->whereCondition($this->conditions) |
|
203
|
|
|
->count(); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Generic constructor. |
|
208
|
|
|
* |
|
209
|
|
|
* @param QueryInterface $query Database query instance |
|
210
|
|
|
*/ |
|
211
|
|
|
public function __construct(QueryInterface $query) |
|
212
|
|
|
{ |
|
213
|
|
|
$this->query = $query; |
|
214
|
|
|
$this->conditions = new Condition(); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
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: