Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,31 @@ Yii2 behaviors implement handling of mongodb embedded documents
===============================================================

* Add attribute with name starting with underscore to model.
~~~
/**
* @inheritdoc
*/
public function attributes()
{
return [
'_address',
'_phones',
]
}
~~~
* Add "safe" validation rule for attribute without underscore in name.
* Use attribute without underscore in name in forms or views
~~~
/**
* @inheritdoc
*/
public function rules()
{
return [
[['address', 'phones'], 'safe'],
]
}
~~~
* Add behavior with attribute name with underscore in name
~~~
'address' => [
'class' => EmbedsOneBehavior::className(),
Expand All @@ -13,33 +36,41 @@ Yii2 behaviors implement handling of mongodb embedded documents
'phones' => [
'class' => EmbedsManyBehavior::className(),
'attribute' => '_phones',
'initEmptyScenarios' => ['create', 'update'],
'embedded' => Phone::className()
],
~~~
* Embedded documents must be inherited from EmbeddedDocument class.
* Your embedded documents must be inherited from [EmbeddedDocument](EmbeddedDocument.php) class.
~~~
class SlaveEmbeddedClass extends EmbeddedDocument
class Phone extends EmbeddedDocument
{
public $name;
public $value;
public $number;
public $type;

public function rules()
{
return [
[['value'], 'boolean', 'on' => 'valueV'],
[['name'], 'integer', 'on' => 'nameV'],
[['name', 'value'], 'safe', 'on'=>'default']
[['number', 'type'], 'string'],
[['type'], 'in', 'range' => ['home', 'work']],
];
}
}
~~~
* To create empty embedded document set base document's scenario to the value listed in initEmptyScenarios parameter of EmbedsManyBehavior
~~~
'address' => [
'class' => EmbedsOneBehavior::className(),
'attribute' => '_address',
'initEmptyScenarios' => ['create', 'update'],
'embedded' => Address::className()
],
~~~
* Use attribute without underscore in form or view
~~~
// Address
echo $form->field($company->address, 'detail');
echo $form->field($company->address, 'id')->hiddenInput();

// Phones
foreach($company->phones as $key => $phone) {
echo $form->field($phone, 'number');
echo $form->field($phone, 'type');
Expand Down