-
Notifications
You must be signed in to change notification settings - Fork 8
/
ActiveRecord.php
459 lines (416 loc) · 14.9 KB
/
ActiveRecord.php
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
namespace devgroup\arangodb;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\ActiveQueryInterface;
use yii\db\BaseActiveRecord;
use yii\db\StaleObjectException;
use yii\helpers\ArrayHelper;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
use triagens\ArangoDb\Document;
abstract class ActiveRecord extends BaseActiveRecord
{
public function mergeAttribute($name, $value)
{
$newValue = $this->getAttribute($name);
if (!is_array($newValue)) {
$newValue === null ? [] : [$newValue];
}
if (is_array($value)) {
$this->setAttribute($name, ArrayHelper::merge($newValue, $value));
} else {
$newValue[] = $value;
$this->setAttribute($name, $newValue);
}
}
public static function collectionName()
{
return Inflector::camel2id(StringHelper::basename(get_called_class()), '_');
}
/**
* Returns the primary key **name(s)** for this AR class.
*
* Note that an array should be returned even when the record only has a single primary key.
*
* For the primary key **value** see [[getPrimaryKey()]] instead.
*
* @return string[] the primary key name(s) for this AR class.
*/
public static function primaryKey()
{
return ['_key'];
}
/**
* Creates an [[ActiveQueryInterface|ActiveQuery]] instance for query purpose.
*
* The returned [[ActiveQueryInterface|ActiveQuery]] instance can be further customized by calling
* methods defined in [[ActiveQueryInterface]] before `one()` or `all()` is called to return
* populated ActiveRecord instances. For example,
*
* ```php
* // find the customer whose ID is 1
* $customer = Customer::find()->where(['id' => 1])->one();
*
* // find all active customers and order them by their age:
* $customers = Customer::find()
* ->where(['status' => 1])
* ->orderBy('age')
* ->all();
* ```
*
* This method is also called by [[BaseActiveRecord::hasOne()]] and [[BaseActiveRecord::hasMany()]] to
* create a relational query.
*
* You may override this method to return a customized query. For example,
*
* ```php
* class Customer extends ActiveRecord
* {
* public static function find()
* {
* // use CustomerQuery instead of the default ActiveQuery
* return new CustomerQuery(get_called_class());
* }
* }
* ```
*
* The following code shows how to apply a default condition for all queries:
*
* ```php
* class Customer extends ActiveRecord
* {
* public static function find()
* {
* return parent::find()->where(['deleted' => false]);
* }
* }
*
* // Use andWhere()/orWhere() to apply the default condition
* // FOR customer IN customer FILTER customer.deleted=:deleted AND customer.age>30 RETURN customer
* $customers = Customer::find()->andWhere('age>30')->all();
*
* // Use where() to ignore the default condition
* // FOR customer IN customer FILTER customer.age>30 RETURN customer
* $customers = Customer::find()->where('age>30')->all();
*
* @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance.
*/
public static function find()
{
/** @var ActiveQuery $query */
$query = \Yii::createObject(ActiveQuery::className(), [get_called_class()]);
$query->from(static::collectionName())->select(static::collectionName());
return $query;
}
/**
* @param ActiveRecord $record
* @param Document|array $row
*/
public static function populateRecord($record, $row)
{
if ($row instanceof Document) {
$row = $row->getAll();
}
parent::populateRecord($record, $row);
}
public function attributes()
{
$class = new \ReflectionClass($this);
$names = [];
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
if (!$property->isStatic()) {
$names[] = $property->getName();
}
}
return $names;
}
/**
* Inserts the record into the database using the attribute values of this record.
*
* Usage example:
*
* ```php
* $customer = new Customer;
* $customer->name = $name;
* $customer->email = $email;
* $customer->insert();
* ```
*
* @param boolean $runValidation whether to perform validation before saving the record.
* If the validation fails, the record will not be inserted into the database.
* @param array $attributes list of attributes that need to be saved. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @param array $options
* @return boolean whether the attributes are valid and the record is inserted successfully.
*/
public function insert($runValidation = true, $attributes = null, $options = [])
{
if ($runValidation && !$this->validate($attributes)) {
return false;
}
$result = $this->insertInternal($attributes, $options);
return $result;
}
protected function insertInternal($attributes = null, $options = [])
{
if (!$this->beforeSave(true)) {
return false;
}
$values = $this->getDirtyAttributes($attributes);
if (empty($values)) {
$currentAttributes = $this->getAttributes();
foreach ($this->primaryKey() as $key) {
$values[$key] = isset($currentAttributes[$key]) ? $currentAttributes[$key] : null;
}
}
$newId = static::getDb()->getDocumentHandler()->save(static::collectionName(), $values);
static::populateRecord($this, static::getDb()->getDocument(static::collectionName(), $newId));
$this->setIsNewRecord(false);
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($this->getAttributes());
$this->afterSave(true, $changedAttributes);
return true;
}
public function update($runValidation = true, $attributeNames = null, $options = [])
{
if ($runValidation && !$this->validate($attributeNames)) {
return false;
}
return $this->updateInternal($attributeNames, $options);
}
protected function updateInternal($attributes = null, $options = [])
{
if (!$this->beforeSave(false)) {
return false;
}
$values = $this->getDirtyAttributes($attributes);
if (empty($values)) {
$this->afterSave(false, $values);
return 0;
}
$condition = $this->getOldPrimaryKey(true);
$lock = $this->optimisticLock();
if ($lock !== null) {
if (!isset($values[$lock])) {
$values[$lock] = $this->$lock + 1;
}
$condition[$lock] = $this->$lock;
}
foreach ($values as $key => $attribute) {
$this->setAttribute($key, $attribute);
}
$rows = (new Query())->options($options)->update(
static::collectionName(),
$values,
[
'_key' => $this->getOldAttribute('_key'),
]
);
if ($lock !== null && !$rows) {
throw new StaleObjectException('The object being updated is outdated.');
}
$changedAttributes = [];
foreach ($values as $name => $value) {
$changedAttributes[$name] = $this->getOldAttribute($name);
$this->setOldAttribute($name, $value);
}
$this->afterSave(false, $changedAttributes);
return $rows;
}
/**
* Returns the connection used by this AR class.
* @return Connection the database connection used by this AR class.
*/
public static function getDb()
{
return \Yii::$app->get('arangodb');
}
protected static function findByCondition($condition)
{
/** @var ActiveQuery $query */
$query = static::find();
if (!ArrayHelper::isAssociative($condition)) {
// query by primary key
$primaryKey = static::primaryKey();
if (isset($primaryKey[0])) {
$collection = static::collectionName();
$condition = ["{$collection}.{$primaryKey[0]}" => $condition];
} else {
throw new InvalidConfigException(get_called_class() . ' must have a primary key.');
}
}
return $query->andWhere($condition);
}
/**
* Updates records using the provided attribute values and conditions.
* For example, to change the status to be 1 for all customers whose status is 2:
*
* ~~~
* Customer::updateAll(['status' => 1], ['status' => '2']);
* ~~~
*
* @param array $attributes attribute values (name-value pairs) to be saved for the record.
* Unlike [[update()]] these are not going to be validated.
* @param array $condition the condition that matches the records that should get updated.
* Please refer to [[QueryInterface::where()]] on how to specify this parameter.
* An empty condition will match all records.
* @param array $options
* @return integer the number of rows updated
*/
public static function updateAll($attributes, $condition = [], $options = [])
{
return (new Query())->options($options)->update(static::collectionName(), $attributes, $condition);
}
/**
* Deletes records using the provided conditions.
* WARNING: If you do not specify any condition, this method will delete ALL rows in the table.
*
* For example, to delete all customers whose status is 3:
*
* ~~~
* Customer::deleteAll([status = 3]);
* ~~~
*
* @param array $condition the condition that matches the records that should get deleted.
* Please refer to [[QueryInterface::where()]] on how to specify this parameter.
* An empty condition will match all records.
* @param array $options
* @return integer the number of rows deleted
*/
public static function deleteAll($condition = [], $options = [])
{
return (new Query())->options($options)->remove(static::collectionName(), $condition);
}
public static function truncate()
{
return static::getDb()->getCollectionHandler()->truncate(static::collectionName());
}
/**
* Saves the current record.
*
* This method will call [[insert()]] when [[getIsNewRecord()|isNewRecord]] is true, or [[update()]]
* when [[getIsNewRecord()|isNewRecord]] is false.
*
* For example, to save a customer record:
*
* ~~~
* $customer = new Customer; // or $customer = Customer::findOne($id);
* $customer->name = $name;
* $customer->email = $email;
* $customer->save();
* ~~~
*
* @param boolean $runValidation whether to perform validation before saving the record.
* If the validation fails, the record will not be saved to database. `false` will be returned
* in this case.
* @param array $attributeNames list of attributes that need to be saved. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @param array $options
* @return boolean whether the saving succeeds
*/
public function save($runValidation = true, $attributeNames = null, $options = [])
{
if ($this->getIsNewRecord()) {
return $this->insert($runValidation, $attributeNames, $options);
} else {
return $this->update($runValidation, $attributeNames, $options) !== false;
}
}
/**
* Deletes the record from the database.
*
* @param array $options
* @return integer|boolean the number of rows deleted, or false if the deletion is unsuccessful for some reason.
* Note that it is possible that the number of rows deleted is 0, even though the deletion execution is successful.
*/
public function delete($options = [])
{
$result = false;
if ($this->beforeDelete()) {
$result = $this->deleteInternal($options);
$this->afterDelete();
}
return $result;
}
/**
* @see ActiveRecord::delete()
* @throws StaleObjectException
*/
protected function deleteInternal($options = [])
{
$condition = $this->getOldPrimaryKey(true);
$lock = $this->optimisticLock();
if ($lock !== null) {
$condition[$lock] = $this->$lock;
}
$result = (new Query())->options($options)->remove(static::collectionName(), $condition);
if ($lock !== null && !$result) {
throw new StaleObjectException('The object being deleted is outdated.');
}
$this->setOldAttributes(null);
return $result;
}
/**
* Returns a value indicating whether the named attribute has been changed.
* @param string $name the name of the attribute
* @return boolean whether the attribute has been changed
*/
public function isAttributeChanged($name, $depth = 2)
{
if (is_array($this->getAttribute($name))) {
$new = $this->getAttribute($name);
$old = $this->getOldAttribute($name);
if ($depth < 1) {
$depth = 1;
}
return self::isArrayChanged($new, $old, $depth);
} else {
return parent::isAttributeChanged($name);
}
}
private static function isArrayChanged(&$new, &$old, $depth)
{
if (is_array($new)) {
if (is_array($old)) {
if (count($new) != count($old)) {
return true;
} else {
$newKeys = array_keys($new);
$oldKeys = array_keys($old);
if (array_merge(array_diff($newKeys, $oldKeys), array_diff($oldKeys, $newKeys))) {
return true;
} else {
if ($depth > 1) {
foreach ($new as $key => $value) {
if (self::isArrayChanged($new[$key], $old[$key], $depth--)) {
return true;
}
}
}
}
}
} else {
return true;
}
} else {
if (is_array($old)) {
return true;
} else {
return (string)$new != (string)$old;
}
}
return false;
}
public function init()
{
parent::init();
if ($this->scenario === static::SCENARIO_DEFAULT) {
$this->setAttributes($this->defaultValues(), false);
}
}
public function defaultValues()
{
return [];
}
}