From f37e113f67632714740a268e3b723e92734ac4cf Mon Sep 17 00:00:00 2001 From: Maxim Date: Mon, 29 Jun 2015 16:54:30 +0300 Subject: [PATCH 1/3] Update README.md --- README.md | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 78f5c85..9655dec 100644 --- a/README.md +++ b/README.md @@ -2,22 +2,38 @@ Yii2 behaviors implement handling of mongodb embedded documents =============================================================== * Add attribute with name starting with underscore to model. +~~~ +/** +* @inheritdoc +*/ +public function attributes() +{ + return [ + '_address', + ] +} +~~~ * 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'], 'safe'], + ] +} +~~~ +* Add behavior with attribute name with underscore in name ~~~ 'address' => [ 'class' => EmbedsOneBehavior::className(), 'attribute' => '_address', 'embedded' => Address::className() ], -'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 { @@ -35,6 +51,14 @@ class SlaveEmbeddedClass extends EmbeddedDocument } ~~~ * 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 ~~~ echo $form->field($company->address, 'detail'); From 8ae7df2d059d4233ac99c11d399f883ee9fe828f Mon Sep 17 00:00:00 2001 From: Maxim Date: Mon, 29 Jun 2015 17:01:23 +0300 Subject: [PATCH 2/3] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9655dec..d627738 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,9 @@ class SlaveEmbeddedClass extends EmbeddedDocument ~~~ echo $form->field($company->address, 'detail'); echo $form->field($company->address, 'id')->hiddenInput(); - +~~~ +or +~~~ foreach($company->phones as $key => $phone) { echo $form->field($phone, 'number'); echo $form->field($phone, 'type'); From 9d5000367fe38fba09f5a3aab900f8aae06719fe Mon Sep 17 00:00:00 2001 From: Maxim Date: Mon, 29 Jun 2015 17:30:09 +0300 Subject: [PATCH 3/3] Update README.md --- README.md | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d627738..d24c6d2 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ public function attributes() { return [ '_address', + '_phones', ] } ~~~ @@ -21,7 +22,7 @@ public function attributes() public function rules() { return [ - [['address'], 'safe'], + [['address', 'phones'], 'safe'], ] } ~~~ @@ -32,20 +33,24 @@ public function rules() 'attribute' => '_address', 'embedded' => Address::className() ], +'phones' => [ + 'class' => EmbedsManyBehavior::className(), + 'attribute' => '_phones', + 'embedded' => Phone::className() +], ~~~ * 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']], ]; } } @@ -61,11 +66,11 @@ class SlaveEmbeddedClass extends EmbeddedDocument ~~~ * Use attribute without underscore in form or view ~~~ +// Address echo $form->field($company->address, 'detail'); echo $form->field($company->address, 'id')->hiddenInput(); -~~~ -or -~~~ + +// Phones foreach($company->phones as $key => $phone) { echo $form->field($phone, 'number'); echo $form->field($phone, 'type');