-
Notifications
You must be signed in to change notification settings - Fork 1
/
RelationBehavior.php
174 lines (159 loc) · 6.08 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
<?php
namespace mdm\relation;
use Yii;
use yii\helpers\ArrayHelper;
/**
* Description of RelationBehavior
*
* @property \yii\db\ActiveRecord $owner Description
* @author Misbahul D Munir (mdmunir) <misbahuldmunir@gmail.com>
*/
class RelationBehavior extends \yii\base\Behavior
{
/**
*
* @param string $relations
* @param array $data
* @param array $options
* @return boolean Description
*/
public function saveRelation($relations, $data, $options = [])
{
$model = $this->owner;
if ($model->load($data)) {
$error = !$model->save();
if (!is_array($relations)) {
$relations = preg_split('/\s*,\s*/', trim($relations), -1, PREG_SPLIT_NO_EMPTY);
}
foreach ($relations as $relationName) {
$error = $this->doSaveRelation($model, $relationName, $data, $options) || $error;
}
return $error ? -1 : 1;
} else {
return false;
}
}
protected function doSaveRelation($model, $relationName, $data, $options)
{
$relation = $model->getRelation($relationName);
// link of relation
$links = [];
foreach ($relation->link as $from => $to) {
$links[$from] = $model[$to];
}
/* @var $class \yii\db\ActiveRecord */
$class = $relation->modelClass;
$multiple = $relation->multiple;
if ($multiple) {
$children = $relation->all();
} else {
$children = $relation->one();
}
$formName = (new $class)->formName();
$postDetails = ArrayHelper::getValue($data, $formName, []);
/* @var $detail \yii\db\ActiveRecord */
$error = false;
if ($multiple) {
$population = [];
foreach ($postDetails as $index => $dataDetail) {
if (isset($options['extra'])) {
$dataDetail = array_merge($options['extra'], $dataDetail);
}
$dataDetail = array_merge($dataDetail, $links);
// primary keys of detail
$detailPks = [];
$pks = $class::primaryKey();
if (count($pks) === 1) {
$detailPks = isset($dataDetail[$pks[0]]) ? $dataDetail[$pks[0]] : null;
} else {
foreach ($pks as $pkName) {
$detailPks[$pkName] = isset($dataDetail[$pkName]) ? $dataDetail[$pkName] : null;
}
}
$detail = null;
// get from current relation
if (empty($relation->indexBy)) {
foreach ($children as $i => $child) {
if ($child->getPrimaryKey() === $detailPks) {
$detail = $child;
unset($children[$i]);
break;
}
}
} elseif (isset($children[$index])) {
$detail = $children[$index];
unset($children[$index]);
}
if ($detail === null) {
$detail = new $class;
}
$detail->load($dataDetail, '');
if (isset($options['beforeValidate'])) {
call_user_func($options['beforeValidate'], $detail, $index);
}
$error = !$detail->validate() || $error;
$population[$index] = $detail;
}
} else {
$population = $children === null ? new $class : $children;
$dataDetail = $postDetails;
if (isset($options['extra'])) {
$dataDetail = array_merge($options['extra'], $dataDetail);
}
$dataDetail = array_merge($dataDetail, $links);
$population->load($dataDetail, '');
if (isset($options['beforeValidate'])) {
call_user_func($options['beforeValidate'], $population, null);
}
$error = !$population->validate() || $error;
}
if (!$error) {
// delete current children before inserting new
if ($multiple) {
$linkFilter = [];
$columns = array_flip($class::primaryKey());
foreach ($relation->link as $from => $to) {
$linkFilter[$from] = $model->$to;
if (isset($columns[$from])) {
unset($columns[$from]);
}
}
$values = [];
if (!empty($columns)) {
$columns = array_keys($columns);
foreach ($children as $child) {
$value = [];
foreach ($columns as $column) {
$value[$column] = $child[$column];
}
$values[] = $value;
}
if (!empty($values)) {
$class::deleteAll(['and', $linkFilter, ['in', $columns, $values]]);
}
} else {
$class::deleteAll($linkFilter);
}
foreach ($population as $index => $detail) {
if (isset($options['beforeSave'])) {
call_user_func($options['beforeSave'], $detail, $index);
}
$detail->save(false);
if (isset($options['afterSave'])) {
call_user_func($options['afterSave'], $detail, $index);
}
}
} else {
if (isset($options['beforeSave'])) {
call_user_func($options['beforeSave'], $population, null);
}
$population->save(false);
if (isset($options['afterSave'])) {
call_user_func($options['afterSave'], $population, null);
}
}
}
$model->populateRelation($relationName, $population);
return $error;
}
}