-
Notifications
You must be signed in to change notification settings - Fork 10
/
RelationBehavior.php
402 lines (379 loc) · 12.5 KB
/
RelationBehavior.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
<?php
namespace mdm\behaviors\ar;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
/**
* Save related
*
* ~~~
* $order = new Order();
*
* // associated array
* $item1 = [
* 'product_id' => 1,
* 'qty' = 10,
* ];
* // as object
* $item2 = new Item();
* $item2->product_id = 2;
*
* $order->items = [
* $item1,
* $item2,
* ];
*
* $order->save();
* ~~~
*
* @property ActiveRecord $owner
*
* @author Misbahul D Munir <[email protected]>
* @since 1.0
*/
class RelationBehavior extends \yii\base\Behavior
{
/**
* @var array scenario for relation
*/
public $relatedScenarios = [];
/**
* @var \Closure callback execute before related validate.
*
* ```php
* function($model,$index,$relationName){
*
* }
* ```
*/
public $beforeRValidate;
/**
* @var \Closure Execute before relation save
* When return false, save will be canceled
* @see [[$beforeRValidate]]
* If function return `false`, save will be canceled
*/
public $beforeRSave;
/**
* @var \Closure Execute after relation save
* @see [[$beforeRValidate]]
*/
public $afterRSave;
/**
* @var boolean If true clear related error
*/
public $clearError = true;
/**
* @var boolean
*/
public $deleteUnsaved = true;
/**
* @var \Closure function to check is two model is equal.
*
* ```
* function ($model1, $model2, $keys){
* return $model1['id'] == $model2['id'];
* }
* ```
*/
public $isEqual;
/**
* @var array
*/
private $_old_relations = [];
/**
* @var array
*/
private $_original_relations = [];
/**
* @var array
*/
private $_process_relation = [];
/**
* @var array
*/
private $_relatedErrors = [];
/**
* @inheritdoc
*/
public function events()
{
return[
ActiveRecord::EVENT_AFTER_VALIDATE => 'afterValidate',
ActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
];
}
/**
* @inheritdoc
*/
public function __set($name, $value)
{
if ($this->setRelated($name, $value) === false) {
parent::__set($name, $value);
}
}
/**
* @inheritdoc
*/
public function canSetProperty($name, $checkVars = true)
{
return $this->owner->getRelation($name, false) !== null || parent::canSetProperty($name, $checkVars);
}
/**
* Populate relation
* @param string $name
* @param array||\yii\db\ActiveRecord||\yii\db\ActiveRecord[] $values
* @return boolean
*/
public function setRelated($name, $values)
{
$relation = $this->owner->getRelation($name, false);
if ($relation === null) {
return false;
}
$class = $relation->modelClass;
$multiple = $relation->multiple;
$link = $relation->link;
$uniqueKeys = array_flip($class::primaryKey());
foreach (array_keys($link) as $from) {
unset($uniqueKeys[$from]);
}
$uniqueKeys = array_keys($uniqueKeys);
if (isset($this->_original_relations[$name])) {
$children = $this->_original_relations[$name];
} else {
$this->_original_relations[$name] = $children = $this->owner->$name;
}
if ($multiple) {
$newChildren = [];
foreach ($values as $index => $value) {
// get from current relation
// if has child with same primary key, use this
/* @var $newChild \yii\db\ActiveRecord */
$newChild = null;
if (empty($relation->indexBy)) {
foreach ($children as $i => $child) {
if ($this->isEqual($child, $value, $uniqueKeys)) {
if ($value instanceof $class) {
$newChild = $value;
$newChild->isNewRecord = $child->isNewRecord;
$newChild->oldAttributes = $child->oldAttributes;
} else {
$newChild = $child;
}
unset($children[$i]);
break;
}
}
} elseif (isset($children[$index])) {
$child = $children[$index];
if ($value instanceof $class) {
$newChild = $value;
$newChild->isNewRecord = $child->isNewRecord;
$newChild->oldAttributes = $child->oldAttributes;
} else {
$newChild = $child;
}
unset($children[$index]);
}
if ($newChild === null) {
$newChild = $value instanceof $class ? $value : new $class;
}
if (isset($this->relatedScenarios[$name])) {
$newChild->scenario = $this->relatedScenarios[$name];
}
if (!$value instanceof $class) {
$newChild->load($value, '');
}
foreach ($link as $from => $to) {
$newChild->$from = $this->owner->$to;
}
$newChildren[$index] = $newChild;
}
$this->_old_relations[$name] = $children;
$this->owner->populateRelation($name, $newChildren);
$this->_process_relation[$name] = true;
} else {
$newChild = null;
if ($children === null) {
if ($values !== null) {
$newChild = $values instanceof $class ? $values : new $class;
$this->_process_relation[$name] = true;
}
} else {
if ($values !== null) {
$newChild = $values instanceof $class ? $values : $children;
if ($values instanceof $class) {
$newChild = $values;
$newChild->oldAttributes = $children->oldAttributes;
$newChild->isNewRecord = $children->isNewRecord;
} else {
$newChild = $children;
}
} else {
$this->_old_relations[$name] = [$children];
}
$this->_process_relation[$name] = true;
}
if ($newChild !== null) {
if (isset($this->relatedScenarios[$name])) {
$newChild->scenario = $this->relatedScenarios[$name];
}
if (!$values instanceof $class) {
$newChild->load($values, '');
}
foreach ($link as $from => $to) {
$newChild->$from = $this->owner->$to;
}
}
$this->owner->populateRelation($name, $newChild);
}
return true;
}
/**
* Handler for event afterValidate
*/
public function afterValidate()
{
/* @var $child \yii\db\ActiveRecord */
foreach ($this->_process_relation as $name => $process) {
if (!$process) {
continue;
}
if ($this->clearError) {
$this->_relatedErrors[$name] = [];
}
$error = false;
$relation = $this->owner->getRelation($name);
$children = $this->owner->$name;
if ($relation->multiple) {
foreach ($children as $index => $child) {
if (isset($this->beforeRValidate)) {
call_user_func($this->beforeRValidate, $child, $index, $name);
}
if (!$child->validate()) {
$errors = $this->_relatedErrors[$name][$index] = $child->getFirstErrors();
$this->owner->addError($name, "{$index}: ".reset($errors));
$error = true;
}
}
} else {
if (isset($this->beforeRValidate)) {
call_user_func($this->beforeRValidate, $children, null, $name);
}
if (!$children->validate()) {
$errors = $this->_relatedErrors[$name] = $child->getFirstErrors();
$this->owner->addError($name, reset($errors));
$error = true;
}
}
if ($error) {
$this->owner->addError($name, 'Related error');
}
}
}
/**
* Handler for event afterSave
*/
public function afterSave()
{
foreach ($this->_process_relation as $name => $process) {
if (!$process) {
continue;
}
// delete old related
/* @var $child \yii\db\ActiveRecord */
if (isset($this->_old_relations[$name])) {
foreach ($this->_old_relations[$name] as $child) {
$child->delete();
}
unset($this->_old_relations[$name]);
}
// save new relation
$relation = $this->owner->getRelation($name);
$link = $relation->link;
$children = $this->owner->$name;
if ($relation->multiple) {
foreach ($children as $index => $child) {
foreach ($link as $from => $to) {
$child->$from = $this->owner->$to;
}
if ($this->beforeRSave === null || call_user_func($this->beforeRSave, $child, $index, $name) !== false) {
$child->save(false);
if (isset($this->afterRSave)) {
call_user_func($this->afterRSave, $child, $index, $name);
}
} elseif ($this->deleteUnsaved && !$child->getIsNewRecord()) {
$child->delete();
}
}
} else {
/* @var $children \yii\db\ActiveRecord */
if ($children !== null) {
foreach ($link as $from => $to) {
$children->$from = $this->owner->$to;
}
if ($this->beforeRSave === null || call_user_func($this->beforeRSave, $children, null, $name) !== false) {
$children->save(false);
if (isset($this->afterRSave)) {
call_user_func($this->afterRSave, $children, null, $name);
} elseif ($this->deleteUnsaved && !$children->getIsNewRecord()) {
$child->delete();
}
}
}
}
unset($this->_process_relation[$name], $this->_original_relations[$name]);
}
}
/**
* Check if relation has error.
* @param string $relationName
* @return boolean
*/
public function hasRelatedErrors($relationName = null)
{
if ($relationName === null) {
foreach ($this->_relatedErrors as $errors) {
if (!empty($errors)) {
return true;
}
}
return false;
} else {
return !empty($this->_relatedErrors[$relationName]);
}
}
/**
* Get related error(s)
* @param string|null $relationName
* @return array
*/
public function getRelatedErrors($relationName = null)
{
if ($relationName === null) {
return $this->_relatedErrors;
} else {
return isset($this->_relatedErrors[$relationName]) ? $this->_relatedErrors[$relationName] : [];
}
}
/**
* Check is boot of model is equal
* @param \yii\db\ActiveRecord|array $model1
* @param \yii\db\ActiveRecord|array $model2
* @param array $keys
* @return boolean
*/
protected function isEqual($model1, $model2, $keys)
{
if ($this->isEqual !== null) {
return call_user_func($this->isEqual, $model1, $model2, $keys);
}
foreach ($keys as $key) {
if (ArrayHelper::getValue($model1, $key) != ArrayHelper::getValue($model2, $key)) {
return false;
}
}
return true;
}
}