-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRelatedAttributeBehavior.php
188 lines (164 loc) · 4.81 KB
/
RelatedAttributeBehavior.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
<?php
namespace iutbay\yii2behaviors;
use Yii;
use yii\base\Event;
use yii\db\ActiveRecord;
/**
* RelatedAttributeBehavior.
*
* @property ActiveRecord $owner
*/
class RelatedAttributeBehavior extends \yii\base\Behavior
{
public $relations = [];
private $_relations = [];
private $_relatedAttributes;
/**
* Make [[$attributes]] writeable
*/
public function __set($name, $value)
{
$this->setRelatedAttribute($name, $value);
}
/**
* Make [[$attributes]] readable
* @inheritdoc
*/
public function __get($name)
{
return $this->getRelatedAttribute($name);
}
/**
* Expose [[$attributes]] writable
* @inheritdoc
*/
public function canSetProperty($name, $checkVars = true)
{
return $this->isRelatedAttribute($name) || parent::canSetProperty($name, $checkVars);
}
/**
* Expose [[$attributes]] readable
* @inheritdoc
*/
public function canGetProperty($name, $checkVars = true)
{
return $this->isRelatedAttribute($name) || parent::canGetProperty($name, $checkVars);
}
/**
* @return boolean
*/
private function isRelatedAttribute($name)
{
$relatedAttributes = $this->getRelatedAttributes();
return isset($relatedAttributes[$name]);
}
/**
* Set related attribute
*/
private function setRelatedAttribute($name, $value)
{
$relatedAttributes = $this->getRelatedAttributes();
$relation = $this->getRelation($relatedAttributes[$name]['relation'], $relatedAttributes[$name]['class']);
$relation->$name = $value;
}
/**
* Get related attribute
* @return mixed
*/
private function getRelatedAttribute($name)
{
$relatedAttributes = $this->getRelatedAttributes();
$relation = $this->getRelation($relatedAttributes[$name]['relation'], $relatedAttributes[$name]['class']);
return $relation->$name;
}
/**
* @param string $name
* @param string $class
* @return ActiveRecord
*/
private function getRelation($name, $class)
{
$relation = $this->owner->__get($name);
if ($relation === null) {
$relation = new $class;
$this->owner->populateRelation($name, $relation);
}
$this->_relations[$name] = $relation;
return $relation;
}
/**
* @return array
*/
public function getRelatedAttributes()
{
if (!isset($this->_relatedAttributes)) {
$relatedAttributes = [];
foreach ($this->relations as $key => $r) {
$getRelation = 'get' . $key;
if (!method_exists($this->owner, $getRelation))
throw new \yii\base\InvalidConfigException("Relation {$key} not found.");
/* @var $activeQuery \yii\db\ActiveQuery */
$activeQuery = $this->owner->$getRelation();
if (is_array($r['attributes'])) {
foreach ($r['attributes'] as $a) {
$relatedAttributes[$a] = [
'relation' => $key,
'class' => $activeQuery->modelClass,
];
}
} else if ($r['attributes'] == '*') {
/* @var $relationModel ActiveRecord */
$relationModel = new $activeQuery->modelClass;
$exclude = array_merge($this->owner->attributes(), $relationModel->primaryKey());
foreach ($relationModel->attributes as $a => $value) {
if (!in_array($a, $exclude)) {
$relatedAttributes[$a] = [
'relation' => $key,
'class' => $activeQuery->modelClass,
];
}
}
}
}
$this->_relatedAttributes = $relatedAttributes;
}
return $this->_relatedAttributes;
}
/**
* @inheritdoc
*/
public function events()
{
return [
ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
];
}
/**
* @param Event $event
*/
public function afterInsert($event)
{
$this->saveRelations();
}
/**
* @param Event $event
*/
public function afterUpdate($event)
{
$this->saveRelations();
}
/**
* Save relations
*/
private function saveRelations()
{
foreach ($this->_relations as $key => $relation) {
if ($relation->isNewRecord) {
$this->owner->link($key, $relation);
} else {
$relation->save();
}
}
}
}