How Set Document Fields In Mongo

I use mongodb in my project and create a class that use this : “yii\mongodb\ActiveRecord” and extended of ActiveRecord.now i have a string field that want to convert it to array before of $model->save() and save in a field of document and as well as add a sub document with it’s params to another field. how can do it for mongodb?

Really No one is here to help me ? :(

Your question is not clear.

The first part is "i have a string field that want to convert it to array before of $model->save() and save in a field of document"

override beforeSave in your ActiveRecord class like:





public function beforeSave($insert)

 {

     if (parent::beforeSave($insert)) {

         // ...you can write your loginc here...

         return true;

     } else {

         return false;

     }

 }




The default implementation will trigger an EVENT_BEFORE_INSERT event when $insert is true, or an EVENT_BEFORE_UPDATE event if $insert is false.

Second part of your question is “as well as add a sub document with it’s params to another field”.

mongodb extension does not provide any special way to work with embedded documents (sub-documents)(So Sad).

Yii Model designed assuming single attribute is a scalar. Validation and attribute processing based on this suggestion. Still any attribute can be an array of any depth and complexity, however you should handle its validation on your own.

you can save a sub document as array in any filed in you mongodb ActiveRecord.




public function beforeSave($insert)

 {

     if (parent::beforeSave($insert)) {

         // ...you can write your loginc here...

         return true;

     } else {

         return false;

     }

 }




thank’s for the answer…