PHP Phalcon → How to send parameters to relational Models in your view

Intro

So I was working on a project where I needed to get the result-set of a model in the controller and send it to the view. Once in the view, if you need to perform a second round of conditioning on your model, you can do the following:

Example

Lets say I have a shoe product on my website and it has 10 pics associated with this shoe. However I only need some of the pics for this particular view on my website.

First in my model I would get the proper information regarding my shoe in the controller:

My models look like this:

So now in the view I will have the details for the Shoes along with all the Images associated with the Shoes. However, I don’t want every shoe picture, just with the roles with a role_id of 44, which in my case is the id that is for the photo gallery. Let’s say in this case I pick the first shoe that has an id in the database as 1.

My view:

If you notice in the view you will see the code on Line 2 as

$details->getImages(‘role_id=44’)

The getImages is a magic method found in Phalcon that allows you to call the model that is related to your main resultset, in this case, Shoes. When you give a parameter such as I did with “role_id=44” it is essentially the same as writing in MySQL:

Select * from Images where role_id=44 AND shoes_id=1

Now in our view, we only have the images that have a role of 44 and are associated with our shoe with an id of 1.

The syntax

Yep….its that easy.

Happy Coding!