Loading Models with INNER JOIN

phalcon

Solution

I'm sure you can use the Query builder for simple joins like:

<?php

//Getting a whole set
$robots = $this->modelsManager->createBuilder()
    ->from('Robots')
    ->join('RobotsParts')
    ->orderBy('Robots.name')
    ->getQuery()
    ->execute();

//Getting the first row
$robots = $this->modelsManager->createBuilder()
    ->from('Robots')
    ->join('RobotsParts')
    ->orderBy('Robots.name')
    ->getQuery()
    ->getSingleResult();

Or PHQL example from the documentation:

<?php

$phql = "SELECT Robots.*
    FROM Robots JOIN RobotsParts p
    ORDER BY Robots.name LIMIT 20";
$result = $manager->executeQuery($phql);

By default, an INNER JOIN is assumed. You can specify the type of JOIN in the query though.

Reference: http://docs.phalconphp.com/en/latest/reference/phql.html#creating-queries-using-the-query-builder

Then I'd overload model's findFirst() method to utilize the above code and assign result values to model's properties.

Problem

Let's say I have a `Phalcon\Mvc\Model` that I load using `::findFirst($id)`. How can I swap in a custom query that would load the model row and do INNER JOIN on some other table? Thanks!

Original source