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

How can I build and access Models Object that are not mapped to a db ?

Hi there,

I consume web service trough a rest API (mostly GET method <=> read data).

GET /country return ["1":"germany","2":"netherland", ...]

GET /size return ["1":"512MB","2":"1GB", ...]

GET /system/55 return ["id":"55","name":"beginner","country":"2", "size":"1", other properties]

I want a CountryModel ($id, $name), a SizeModel ($id, $size) and a SystemModel ($id, $name, $country_id, $size_id ...) , with relations between them (system hasOne size, system hasOne country ...)

What I want to achieve is something like: SystemModel::findFirst("55")->country->name

I imagine that I will have to use setter and getters for my models properties but how to query them and use relationships ???

Maybe implement something that will work like Interface Phalcon\Db\ResultInterface ???

I'm kinda in the same place with one of my projects. Ideally you would want to use the ODM and the Collection class (json is json right?), but currently it's written very much for MongoDB. You MIGHT be able to hack around with the ODM CollectionInterface, but you will end up writing a class that really doesn't benefit much from the framework itself other than having the same names for the functions. Your save(), delete(), find(), and findFirst() will all be API calls and I'm pretty sure you would have to write your own hydration routines, though I could be wrong on that last point. If you do manage to get it all working with models/collections, let me know!



43.9k

Hi, thanks for posting some interesting ideas. Is it possible to define relations between ODM models as with db models ?

But in fact, ... I do believe that achieving this kind of task is over my basic knowledge !