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

Is it possible to query slim model classes?

Hello,

I was digging around in the docs for some time but could not find an answer to this: Is it possible to tell a "Robots" class ( which extends \Phalcon\Mvc\Model ) to return simple "Robot" objects that do not extend \Phalcon\Mvc\Model ?

I'm asking because I'm working on a spatial application that will handle 10000s of objects at runtime. And it will make a significant impact on memory usage when I could query slim "Robot" objects instead of fat "Robots" objects.

Regards



12.1k

Hi dempie,

I would say this is not a question to the phalcon framework than to php in general. If you only neet the database interaction from the \Phalcon\Mvc\Model than you could try to create an extra class "RobotsOne" with no parent with the method getRobot. In the getRobot you fetch all the data you need from the Robots class which is a child of \Phalcon\Mvc\Model.

greetings Eike



8.6k

Thanks, this sounds quite reasonable to overwrite the \Phalcon\Mvc\Model methods to return simple "Robot" classes (if that's what you pointing at). I'll give it a try.



12.1k

No I don't mean to overwrite the \Phalcon\Mvc\Model. You create a very new class which has no parent and has the method getRobot. In this method you create a new instance of Robots and return it. This Robots is a child of \Phalcon\Mvc\Model. Hope this was clearer ;)



8.6k

Ah, yes. This would be also another solution. But I'd prefer to overload / add find function(s) that return slim objects.



650
Accepted
answer

I'm sorry if I am missing something, but isn't this what the hydration mode refers to? See at: https://docs.phalcon.io/en/latest/reference/models.html#hydration-modes. So in your case the smallest result you can return is HYDRATE::ARRAYS



8.6k

Ah, yes. This also decreases memory usage when retrieving objects from DB - thanks for the pointer.