-
Notifications
You must be signed in to change notification settings - Fork 10
/
RelationTrait.php
352 lines (339 loc) · 12.6 KB
/
RelationTrait.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
<?php
namespace mdm\behaviors\ar;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* Description of RelationTrait
*
* @property array $relatedErrors
* @property array $relatedErrorMessages
*
* @author Misbahul D Munir <[email protected]>
* @since 1.0
*/
trait RelationTrait
{
/**
* @var array
*/
private $_old_relations = [];
/**
* @var array
*/
private $_original_relations = [];
/**
* @var array
*/
private $_process_relation = [];
/**
* @var array
*/
private $_relatedErrors = [];
/**
* @var array
*/
private $_relatedErrorMessages = [];
public function afterValidate()
{
$this->doAfterValidate();
parent::afterValidate();
}
public function afterSave($insert, $changedAttributes)
{
$this->doAfterSave();
parent::afterSave($insert, $changedAttributes);
}
/**
*
* @return array
*/
public function getRelatedErrors()
{
return $this->_relatedErrors;
}
/**
* Can be use to update ActiveForm message
* @return type
*/
public function getRelatedErrorMessages()
{
return $this->_relatedErrorMessages;
}
/**
* Populate relation
* @param string $name
* @param array||ActiveRecord||ActiveRecord[] $values
* @return boolean
*/
public function loadRelated($name, $values)
{
$relation = $this->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->$name;
}
if ($multiple) {
$newChildren = [];
$values = $values ?: [];
foreach ($values as $index => $value) {
// get from current relation
// if has child with same primary key, use this
/* @var $newChild ActiveRecord */
$newChild = null;
if (empty($relation->indexBy)) {
foreach ($children as $i => $child) {
if ($this->childIsEqual($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, $this->relatedScenarios[$name])) {
$newChild->scenario = $this->relatedScenarios[$name];
}
if (!$value instanceof $class) {
$newChild->load($value, '');
}
foreach ($link as $from => $to) {
$newChild->$from = $this->$to;
}
$newChildren[$index] = $newChild;
}
$this->_old_relations[$name] = $children;
$this->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->$to;
}
}
$this->populateRelation($name, $newChild);
}
return true;
}
/**
* Handler for event afterValidate
*/
protected function doAfterValidate()
{
$handleValidate = method_exists($this, 'beforeRValidate');
/* @var $child ActiveRecord */
foreach ($this->_process_relation as $name => $process) {
if (!$process) {
continue;
}
if (!isset($this->clearError) || $this->clearError) {
$this->_relatedErrors[$name] = [];
$this->_relatedErrorMessages[$name] = [];
}
$error = false;
$relation = $this->getRelation($name);
$children = $this->$name;
if ($relation->multiple) {
foreach ($children as $index => $child) {
if ($handleValidate) {
$this->beforeRValidate($child, $index, $name);
}
$event = new RelationEvent([
'child' => $child,
'relation' => $name,
'index' => $index,
]);
$this->trigger(RelationEvent::BEFORE_VALIDATE, $event);
if (!$child->validate()) {
$errors = $this->_relatedErrors[$name][$index] = $child->getFirstErrors();
foreach ($errors as $attribute => $message) {
$this->_relatedErrorMessages[$name][Html::getInputId($child, "[$index]$attribute")] = $message;
}
$this->addError($name, "{$name}[{$index}]: " . reset($errors));
$error = true;
}
}
} else {
if ($handleValidate) {
$this->beforeRValidate($children, null, $name);
}
$event = new RelationEvent([
'child' => $child,
'relation' => $name,
]);
$this->trigger(RelationEvent::BEFORE_VALIDATE, $event);
if (!$children->validate()) {
$errors = $this->_relatedErrors[$name] = $child->getFirstErrors();
foreach ($errors as $attribute => $message) {
$this->_relatedErrorMessages[$name][Html::getInputId($child, $attribute)] = $message;
}
$this->addError($name, "$name: " . reset($errors));
$error = true;
}
}
}
}
/**
* Handler for event afterSave
*/
protected function doAfterSave()
{
$handleBefore = method_exists($this, 'beforeRSave');
$handleAfter = method_exists($this, 'afterRSave');
$deleteUnsaved = isset($this->deleteUnsaved) ? $this->deleteUnsaved : true;
foreach ($this->_process_relation as $name => $process) {
if (!$process) {
continue;
}
$delUnsaved = is_array($deleteUnsaved) ? (isset($deleteUnsaved[$name]) ? $deleteUnsaved[$name] : true) : $deleteUnsaved;
// delete old related
/* @var $child 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->getRelation($name);
$link = $relation->link;
$children = $this->$name;
if ($relation->multiple) {
foreach ($children as $index => $child) {
foreach ($link as $from => $to) {
$child->$from = $this->$to;
}
$oke = $handleBefore === false || $this->beforeRSave($child, $index, $name) !== false;
$event = new RelationEvent([
'child' => $child,
'relation' => $name,
'index' => $index,
]);
$this->trigger(RelationEvent::BEFORE_SAVE, $event);
$oke = $oke && $event->isValid;
if ($oke) {
$child->save(false);
if ($handleAfter) {
$this->afterRSave($child, $index, $name);
}
$event = new RelationEvent([
'child' => $child,
'relation' => $name,
'index' => $index,
]);
$this->trigger(RelationEvent::AFTER_SAVE, $event);
} elseif ($delUnsaved && !$child->getIsNewRecord()) {
$child->delete();
}
}
} else {
/* @var $children ActiveRecord */
if ($children !== null) {
foreach ($link as $from => $to) {
$children->$from = $this->$to;
}
$oke = $handleBefore === false || $this->beforeRSave($children, null, $name) !== false;
$event = new RelationEvent([
'child' => $children,
'relation' => $name,
]);
$this->trigger(RelationEvent::BEFORE_SAVE, $event);
$oke = $oke && $event->isValid;
if ($oke) {
$children->save(false);
if ($handleAfter) {
$this->afterRSave($children, null, $name);
}
$event = new RelationEvent([
'child' => $children,
'relation' => $name,
]);
$this->trigger(RelationEvent::AFTER_SAVE, $event);
} elseif ($delUnsaved && !$children->getIsNewRecord()) {
$child->delete();
}
}
}
unset($this->_process_relation[$name], $this->_original_relations[$name]);
}
}
/**
* Check is boot of model is equal
* @param ActiveRecord|array $model1
* @param ActiveRecord|array $model2
* @param array $keys
* @return boolean
*/
protected function childIsEqual($model1, $model2, $keys)
{
if (method_exists($this, 'isEqual')) {
return $this->isEqual($model1, $model2, $keys);
}
if (empty($keys)) {
return false;
}
foreach ($keys as $key) {
if (ArrayHelper::getValue($model1, $key) != ArrayHelper::getValue($model2, $key)) {
return false;
}
}
return true;
}
}