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

Search by foreign keys

Hello.

Can you tell if i can search by a foreign key with find() method ? Or i should create a custom query for that ?



98.9k

You can use a query builder for that:

$robots = $this->modelsManager->createBuilder()
                ->from('Robots')
                ->join('Parts')
                ->where('Parts.type <> "some-type"');


51.2k

Thank you. In my app i have to run complex queries, and i think i will choose to write them plain. What would be a good practice to run complex queries in models, without using directly the pdo ?

Now i have something like:

    $db = $this->getDI()->get('db')->getInternalHandler();
    $s_query = '...';
    $result  = $db->query($s_query);
    $result->setFetchMode(\PDO::FETCH_OBJ);

    return $result->fetchAll();

PS: I think it would be nice and helpful to have a section in the documentation, like "Good practices"