Shahin Ataei
Shahin Ataei

Reputation: 25

Phalcon: How can i execute my Inner join statement with query builder

How can i execute this statement with "Query builder" in phalcon framework and fetch it result:

SELECT Table2.Id 
FROM Table2 INNER Table1 
WHERE Table1.Id = 1 
AND Table2.Name = "Shahin" 
AND Table1.Max > Table2.Count;

Tables

Upvotes: 0

Views: 1978

Answers (1)

Nikolay Mihaylov
Nikolay Mihaylov

Reputation: 3876

Answering a bit late, but let's hope it helps someone else. Phalcon provides an awesome Query builder functionality. You can read more here: https://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_Query_Builder.html

And here is sample join query:

$result = $this->modelsManager->createBuilder()
   ->columns(array('main.*', 'locations.*'))
   ->from(array('main' => 'Models\Objects'))
   ->leftJoin('Models\ObjectLocations', 'locations.foreign_key = main.id', 'locations')
   ->where('main.active = 1')
   ->getQuery()->execute();

This will return two objects if query is successful and you selected all the fields (*) from a table:

$result->main
$result->locations

Good thing is those objects allow you to use all model methods. For example

$result->locations->getDistanceBlabla()

Upvotes: 1

Related Questions