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

"Find" Use "column" how to use the Relationships between Models

"Find" Use "column" how to use the Relationships between Models



9.8k
edited Sep '15

I am still learning. I understand that you mean relations like hasMany, hasManyToMany belongsTo, (virtual foreign keys), cascade removal, much more. I think it is good to read at least all about Models (it's long but it pays off to read it), and also about phql. The relationships appear there from time to time. https://docs.phalcon.io/en/latest/reference/models.html#relationships-between-models https://docs.phalcon.io/en/latest/reference/phql.html Hope it will help you.

F.e. You can get the results quite automatically expressed in models when you select all columns from a model when this model is connected to another second model (hasMany or belongsTo if i am right) and if additionallyh that model is connected to another - the third one and using hasMany or hasMany to many allows you to get the information somehow connected to your model. Whenever you use not all columns to be selected not full Model is returned (you cannot save it using ->save() for example) but a collection of objects.

If you return some records with joined model just use ::query static method from model if you DONT wanna get same columns(for some reason you cant alias current model using ::query) or just modelsManager which allow you for doing pretty much anything.