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 in 3 tables

How do i Make a query in Phalcon that search in 3 diffrent tables?

Thanks!



351
Accepted
answer
edited Oct '15

If it is a join you are looking to do, you can do it in this manner inside your controller:

$result = $this->modelsManager->executeQuery(
    'SELECT * FROM \Placeholder\Models\ModelOne m1, ' .
    '\Placeholder\Models\ModelTwo m2 ' .
    '\Placeholder\Models\ModelThree m3 ' .
    'WHERE m1.id=m2.id AND m2.id=m3.id' .
    'AND m3.id=:id:',
    ['id' => $id]
);

I dont think it can be done with Join..

Yes it can, the code that Tobias posted would result in a Complex Result set - If you call ->toArray() against it, you'll find how the SQL returned is structured.

I think you need something like LEFT JOIN if you are using mysql? Just need to make sure each table has an id each can refer to.