We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Adding another subdocument in a specific document without overwriting?

Hy

I am trying to add a subdocument to an array of documents. I have a collection with the following structure:

id name comments [ {name: fool, comment: fool}, {name: foolTwo, comment: foolTwo} ]

When I try to add another subdocument it replaces the existing one. In pure code I would like this:

$ collection-> update ( array ('_id' => new Mongoid ($ id)), array ('$ push' => array ("comments" => array ("name" => $ name, 'comment' => $ text,))));

How do I do this in the Phalcon?

Thank you for attention.



3.6k

To update a subdocument:

  1. Create your model and call find/findFirst to populate it with data based on some condition (for example, _id)
  2. Add the new comment to the comments array.
  3. Save

it works! Thanx.