forked from consultnn/yii2-mongodb-embedded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmbedsOneBehavior.php
45 lines (40 loc) · 1.07 KB
/
EmbedsOneBehavior.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
<?php
namespace consultnn\embedded;
use yii\helpers\Html;
/**
* Class EmbeddedOneBehavior
* @property EmbeddedDocument $storage
* @package common\behaviors
*/
class EmbedsOneBehavior extends AbstractEmbeddedBehavior
{
protected function setAttributes($attributes, $safeOnly = true)
{
$this->storage->scenario = $this->owner->scenario;
$this->storage->setAttributes($attributes, $safeOnly);
}
/**
* @inheritdoc
*/
protected function getAttributes()
{
if ($this->saveEmpty || !$this->storage->isEmpty())
return $this->storage->attributes;
else
return null;
}
/**
* @return EmbeddedDocument
*/
public function getStorage()
{
if (empty($this->_storage)) {
$this->_storage = $this->createEmbedded(
(array)$this->owner->{$this->attribute},
false,
['formName' => $this->setFormName ? Html::getInputName($this->owner, $this->fakeAttribute) : null]
);
}
return $this->_storage;
}
}