1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\ember\records; |
10
|
|
|
|
11
|
|
|
use craft\helpers\Json; |
12
|
|
|
use flipbox\craft\ember\exceptions\RecordNotFoundException; |
13
|
|
|
use flipbox\craft\ember\models\DateCreatedRulesTrait; |
14
|
|
|
use flipbox\craft\ember\models\DateUpdatedRulesTrait; |
15
|
|
|
use flipbox\craft\ember\models\UidRulesTrait; |
16
|
|
|
use yii\base\InvalidConfigException; |
17
|
|
|
use yii\db\ActiveQueryInterface; |
18
|
|
|
use yii\helpers\ArrayHelper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This class provides additional functionality to Craft's ActiveRecord: |
22
|
|
|
* |
23
|
|
|
* Table Alias - By default the table alias is the name of the table, without the opening '{{%' and closing '}}' |
24
|
|
|
* syntax. Additionally, the table alias (and therefore table name) is set via a constant. |
25
|
|
|
* |
26
|
|
|
* Audit Attributes - Craft defines 'dateCreated', 'dateUpdated' and 'UID' as audit attributes which are automatically |
27
|
|
|
* accounted for. It is important to note that these attributes are also set as 'safe' for the 'default' scenario; |
28
|
|
|
* therefore making them easily set-able and accessible. |
29
|
|
|
* |
30
|
|
|
* Getter Priority Attributes - When set, the attribute will call the 'getter' method instead of the traditional |
31
|
|
|
* 'getAttribute' method. Examine the case when using relational Id attributes; we'll use 'userId' as an example. |
32
|
|
|
* Calling the attribute directly `$this->userId` would call `$this->getUserId()` which should look at the relation |
33
|
|
|
* `$this->user->getId()` to ensure the related object and Id are the same. Commonly, this is useful when continuity |
34
|
|
|
* between the Id an object need to be upheld. |
35
|
|
|
* |
36
|
|
|
* @author Flipbox Factory <[email protected]> |
37
|
|
|
* @since 2.0.0 |
38
|
|
|
*/ |
39
|
|
|
abstract class ActiveRecord extends \craft\db\ActiveRecord |
40
|
|
|
{ |
41
|
|
|
use DateCreatedRulesTrait, |
42
|
|
|
DateUpdatedRulesTrait, |
43
|
|
|
UidRulesTrait; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* These attributes will have their 'getter' methods take priority over the normal attribute lookup. It's |
47
|
|
|
* VERY important to note, that the value returned from the getter should NEVER be different than the raw |
48
|
|
|
* attribute value set. If, for whatever reason, the getter determines the attribute value is |
49
|
|
|
* incorrect, it should set the new value prior to returning it. |
50
|
|
|
* |
51
|
|
|
* These getters are commonly used to ensure an associated model and their identifier are in sync. For example, |
52
|
|
|
* a userId attribute and a user object (with an id attribute). An operation may have saved and set an Id on the |
53
|
|
|
* model, but the userId attribute remains null. The getter method may check (and set) the value prior to |
54
|
|
|
* returning it. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $getterPriorityAttributes = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* These attributes will have their 'setter' methods take priority over the normal attribute setting. |
62
|
|
|
* |
63
|
|
|
* These setters are commonly used to ensure an associated model and their identifier are in sync. For example, |
64
|
|
|
* a userId attribute and a user object (with an id attribute). |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $setterPriorityAttributes = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The table alias |
72
|
|
|
*/ |
73
|
|
|
const TABLE_ALIAS = ''; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public static function tableAlias() |
79
|
|
|
{ |
80
|
|
|
return static::TABLE_ALIAS; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public static function tableName() |
87
|
|
|
{ |
88
|
|
|
return '{{%' . static::tableAlias() . '}}'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/******************************************* |
93
|
|
|
* OVERRIDE CONDITION HANDLING |
94
|
|
|
*******************************************/ |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Finds ActiveRecord instance(s) by the given condition. |
98
|
|
|
* This method is internally called by [[findOne()]] and [[findAll()]]. |
99
|
|
|
* @param mixed $condition please refer to [[findOne()]] for the explanation of this parameter |
100
|
|
|
* @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance. |
101
|
|
|
* @throws InvalidConfigException if there is no primary key defined. |
102
|
|
|
* @internal |
103
|
|
|
*/ |
104
|
|
|
protected static function findByCondition($condition) |
105
|
|
|
{ |
106
|
|
|
$query = static::find(); |
107
|
|
|
|
108
|
|
|
if (!empty($condition) && !ArrayHelper::isAssociative($condition)) { |
109
|
|
|
// query by primary key |
110
|
|
|
$primaryKey = static::primaryKey(); |
111
|
|
|
if (isset($primaryKey[0])) { |
112
|
|
|
$pk = $primaryKey[0]; |
113
|
|
|
if (!empty($query->join) || !empty($query->joinWith)) { |
114
|
|
|
$pk = static::tableName() . '.' . $pk; |
115
|
|
|
} |
116
|
|
|
// if condition is scalar, search for a single primary key, if it is array, search for |
117
|
|
|
// multiple primary key values |
118
|
|
|
$condition = [$pk => is_array($condition) ? array_values($condition) : $condition]; |
119
|
|
|
} else { |
120
|
|
|
throw new InvalidConfigException('"' . get_called_class() . '" must have a primary key.'); |
121
|
|
|
} |
122
|
|
|
} elseif (is_array($condition)) { |
123
|
|
|
foreach ($condition as $key => $value) { |
124
|
|
|
if ($query->canSetProperty($key)) { |
125
|
|
|
$query->{$key} = $value; |
126
|
|
|
unset($condition[$key]); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** @noinspection PhpInternalEntityUsedInspection */ |
131
|
|
|
$condition = static::filterCondition($condition); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $query->andWhere($condition); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/******************************************* |
139
|
|
|
* FIND |
140
|
|
|
*******************************************/ |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritdoc |
144
|
|
|
*/ |
145
|
|
|
public static function findOne($condition) |
146
|
|
|
{ |
147
|
|
|
if ($condition instanceof self) { |
148
|
|
|
return $condition; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return parent::findOne($condition); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
/******************************************* |
156
|
|
|
* GET |
157
|
|
|
*******************************************/ |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param $condition |
161
|
|
|
* @return static |
162
|
|
|
* @throws RecordNotFoundException |
163
|
|
|
*/ |
164
|
|
|
public static function getOne($condition) |
165
|
|
|
{ |
166
|
|
|
if (null === ($record = static::findOne($condition))) { |
167
|
|
|
throw new RecordNotFoundException( |
168
|
|
|
sprintf( |
169
|
|
|
"Record not found with the following condition: %s", |
170
|
|
|
Json::encode($condition) |
171
|
|
|
) |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** @var static $record */ |
176
|
|
|
|
177
|
|
|
return $record; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param $condition |
182
|
|
|
* @return static[] |
183
|
|
|
* @throws RecordNotFoundException |
184
|
|
|
*/ |
185
|
|
|
public static function getAll($condition) |
186
|
|
|
{ |
187
|
|
|
$records = static::findAll($condition); |
188
|
|
|
|
189
|
|
|
if (empty($records)) { |
190
|
|
|
throw new RecordNotFoundException( |
191
|
|
|
sprintf( |
192
|
|
|
"Records not found with the following condition: %s", |
193
|
|
|
Json::encode($condition) |
194
|
|
|
) |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $records; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/******************************************* |
203
|
|
|
* RULES |
204
|
|
|
*******************************************/ |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @inheritdoc |
208
|
|
|
*/ |
209
|
|
|
public function rules() |
210
|
|
|
{ |
211
|
|
|
return array_merge( |
212
|
|
|
parent::rules(), |
213
|
|
|
$this->dateCreatedRules(), |
214
|
|
|
$this->dateUpdatedRules(), |
215
|
|
|
$this->uidRules() |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/******************************************* |
220
|
|
|
* ATTRIBUTES |
221
|
|
|
*******************************************/ |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param string $name |
225
|
|
|
* @return mixed |
226
|
|
|
*/ |
227
|
|
|
public function __get($name) |
228
|
|
|
{ |
229
|
|
|
if ($this->hasGetterPriority($name)) { |
230
|
|
|
$this->{'get' . $name}(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return parent::__get($name); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param string $name |
238
|
|
|
* @param mixed $value |
239
|
|
|
*/ |
240
|
|
|
public function __set($name, $value) |
241
|
|
|
{ |
242
|
|
|
if ($this->hasSetterPriority($name)) { |
243
|
|
|
$this->{'set' . $name}($value); |
244
|
|
|
return; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
parent::__set($name, $value); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @inheritdoc |
252
|
|
|
*/ |
253
|
|
|
public function getDirtyAttributes($names = null) |
254
|
|
|
{ |
255
|
|
|
$attributes = $names ?: $this->getterPriorityAttributes; |
256
|
|
|
|
257
|
|
|
// Call each attribute to see if the 'getter' has a value |
258
|
|
|
foreach ($attributes as $attribute) { |
259
|
|
|
if ($this->hasGetterPriority($attribute)) { |
260
|
|
|
$this->{'get' . $attribute}(); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return parent::getDirtyAttributes($names); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param $name |
269
|
|
|
* @return bool |
270
|
|
|
*/ |
271
|
|
|
protected function hasSetterPriority($name) |
272
|
|
|
{ |
273
|
|
|
return in_array($name, $this->setterPriorityAttributes, true) && |
274
|
|
|
method_exists($this, 'set' . $name); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param $name |
279
|
|
|
* @return bool |
280
|
|
|
*/ |
281
|
|
|
protected function hasGetterPriority($name) |
282
|
|
|
{ |
283
|
|
|
return in_array($name, $this->getterPriorityAttributes, true) && |
284
|
|
|
method_exists($this, 'get' . $name); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|