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

Phalcon Relation

I'm wondering how to use the model based on a column in it's foreign key.

In SQL it would be similar to: SELECT a.id, b.* FROM a JOIN b ON a.col = b.col WHERE b.col = "val"

If I want to translate that to using Phalcon models similar to A::find("conditions" => "a.id = :id:, b.col = :val:") will not work.

Relation between A to B is 1 to many

Is the solution to use -find A instance on id -query "a"->b(//can I insert query parameters here)?

Not sure exactly what you are looking to do here. Are you trying to look for rows of "b" that belong to "a"? You have this:

JOIN b ON a.col = b.col WHERE b.col = "val"

Did you really mean that you were searching for a different b column? Like WHERE b.col2 = "val"? It might be easier if we were a little less generic with the names. For instance, are you looking to find all the orders for a customer where teh customer ordered a specific item?

SELECT customer.id, order.* FROM customer INNER JOIN order ON customer.id = order.customer_id WHERE order.product_id = 3

Sorry if I'm not understanding, I'm just trying to find out exactly what data you are looking for and the relation between the tables.

If you are looking a a specific customer ("a"), and would like to get a specific subset of orders ("b"), then you can do something like:

$customer = Customers::findFirst($id); $products = $customer->getOrders('product_id='.$product_id);

The getOrders() function is a magic get that you can use if you have a defined relationship to the Orders model. You will need an Orders model defined. Take a look the docs for further info: https://phalcon-php-framework-documentation.readthedocs.org/en/latest/reference/models.html#taking-advantage-of-relationships



652

Oh no, you got my meaning. Sorry I didn't realize I had named both col. So I can have conditions in the getOrders() method, which was my question. I have the models and relationships defined. Thanks!