Skip to content

Commit f94d153

Browse files
committed
возможность не сохранять пустые модели
1 parent 49df36c commit f94d153

File tree

4 files changed

+84
-11
lines changed

4 files changed

+84
-11
lines changed

AbstractEmbeddedBehavior.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ abstract class AbstractEmbeddedBehavior extends Behavior
3333
*/
3434
public $setFormName = true;
3535

36+
/**
37+
* Save empty embedded models to db. Emptyness is checked via EmbeddedDocument::isEmpty method.
38+
*
39+
* Сохранять ли пустые вложенные объекты в бд. Проверка на пустоту делается в методе EmbeddedDocument::isEmpty.
40+
* @see EmbeddedDocument::isEmpty
41+
* @var bool
42+
*/
43+
public $saveEmpty = true;
44+
3645
/**
3746
* @var mixed
3847
*/
@@ -51,6 +60,14 @@ abstract function getStorage();
5160
*/
5261
abstract protected function setAttributes($attributes, $safeOnly = true);
5362

63+
/**
64+
* Return storage attributes.
65+
*
66+
* Возвращает данные для сохранения в бд.
67+
* @return mixed
68+
*/
69+
abstract protected function getAttributes();
70+
5471
public function events()
5572
{
5673
return [
@@ -97,7 +114,7 @@ public function proxy($event)
97114
{
98115
$this->storage->setScenario($this->owner->scenario);
99116
$this->storage->trigger($event->name, $event);
100-
$this->owner->{$this->attribute} = $this->storage->attributes;
117+
$this->owner->{$this->attribute} = $this->getAttributes();
101118
}
102119

103120
/**

EmbeddedDocument.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,38 @@ public function setScenario($scenario)
102102
parent::setScenario($scenario);
103103
}
104104
}
105+
106+
/**
107+
* Checks if embedded model is empty.
108+
* Doesn't take into account validator's "when" and "isEmpty" parameters.
109+
*
110+
* Проверяет объект на пустоту.
111+
* Пустым считается объект все атрибуты которого пусты (empty($value)) или равны значениям по-умолчанию. Не учитывает параметры "when" и "isEmpty" валидаторов "по-умолчанию".
112+
* @return bool
113+
*/
114+
public function isEmpty()
115+
{
116+
$notEmptyAttributes = [];
117+
foreach ($this->attributes() as $atrribute)
118+
{
119+
if (!empty($this->$atrribute))
120+
$notEmptyAttributes[$atrribute] = $atrribute;
121+
}
122+
123+
foreach ($this->getActiveValidators() as $validator)
124+
{
125+
if (($validator instanceof \yii\validators\DefaultValueValidator) && ($checkAttributes = array_intersect($validator->attributes, $notEmptyAttributes)))
126+
{
127+
/** @var \yii\validators\DefaultValueValidator $validator */
128+
129+
foreach ($checkAttributes as $atrribute)
130+
{
131+
if ($this->$atrribute == $validator->value)
132+
unset($notEmptyAttributes[$atrribute]);
133+
}
134+
}
135+
}
136+
137+
return empty($notEmptyAttributes);
138+
}
105139
}

EmbedsManyBehavior.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,35 @@ public function getFormName($index)
2222
}
2323
}
2424

25+
/**
26+
* @inheritdoc
27+
*/
2528
protected function setAttributes($attributes, $safeOnly = true)
2629
{
2730
$this->storage->removeAll();
2831

29-
if (!empty($attributes)) {
30-
foreach($attributes as $modelAttributes) {
31-
$model = $this->createEmbedded(
32-
$modelAttributes,
33-
$safeOnly,
34-
['formName' => $this->getFormName($this->storage->getNextIndex())]
35-
);
32+
if (empty($attributes))
33+
return;
34+
35+
foreach($attributes as $modelAttributes) {
36+
$model = $this->createEmbedded(
37+
$modelAttributes,
38+
$safeOnly,
39+
['formName' => $this->getFormName($this->storage->getNextIndex())]
40+
);
41+
42+
if ($this->saveEmpty || !$model->isEmpty()) {
3643
$this->storage[] = $model;
3744
}
38-
} else {
39-
$this->storage->removeAll();
4045
}
46+
}
4147

42-
48+
/**
49+
* @inheritdoc
50+
*/
51+
protected function getAttributes()
52+
{
53+
return $this->storage->attributes;
4354
}
4455

4556
/**

EmbedsOneBehavior.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ protected function setAttributes($attributes, $safeOnly = true)
1717
$this->storage->setAttributes($attributes, $safeOnly);
1818
}
1919

20+
/**
21+
* @inheritdoc
22+
*/
23+
protected function getAttributes()
24+
{
25+
if ($this->saveEmpty || !$this->storage->isEmpty())
26+
return $this->storage->attributes;
27+
else
28+
return null;
29+
}
30+
2031
/**
2132
* @return EmbeddedDocument
2233
*/

0 commit comments

Comments
 (0)