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

Using Mongo in an MVC proyect

So, I'm dealing with a problem trying to use MongoDB.

In my controller I'm doing a query, something like Sites::find(); This is returning an array of Phalcon\Mvc\Collection\Document . In a normal relational database what I get is resultset, Phalcon\Mvc\Model\Resultset\Simple.

The problem is that I want to use the result I'm getting from my controller in my .volt files. I'm not able to do it, since clearly are different sort of results. But this is not really clear to understand...

I want to use the result I'm getting from mongo to do something like:

{{ select("domain", domains, "using" : ["_id", "template"]) }}

Where of course domains is the results that I'm getting from mongo. Is there anyway to convert my result in something that the view is able to understand and render properly???

Please ideas


Another question is, can I addBehaviors to Collections???

Seems is not possible so far. I'm confuse what is the way to work with Mongo and Phalcon...

It seems strange to me that models extending Collection don't support the same framework features as models which extend Model. I would have thought things like Behaviors and volt form rendering would work for either out of the box.



Btw, do not forget you work with php. Therefore, extend away and add whatever you need.

So, in other words, the framework is not expecting for Collections to function on equal footing with models now or at any point in the future?


Right now, it is more of a wrapper of the mongo driver with some extras (events, etc). I do not know about the future, but I have a suspicion that it will become more of a priority considering the aggressive MongoDB Inc politics these days for "world domination," the wider adoption of non-relational databases, and the cheaper VPS hosting.

I tried to boil all down to an array, since both Collection and ResultSet inherit from ArrayAccess. As lobostome said: You have all possibilities.


Hello Sebastian

You can add behaviors in a collections for Example you can visit https://zappysys.com/blog/ssis-loading-data-into-mongodb-upsert-update-delete-insert/

I hope it will help you.