Question

If we want to join multiple tables into single query using query builder in Phalcon. It demands to have models created for all tables that need to be joined.

Is this drawback or advantage?

$m = Firstmodel::query()
    ->columns("col1, col2")

    ->where("col3 = '2'")

    ->join("model2", "col1 = col5", 'a')
    ->join("model3", "col6 = col7", 'b')

    ->orderBy("col1")
    ->execute();

With one join query it works perfectly. But having 2 join queries like above gives be below error:

Scanning error before '] JOIN [] JOIN [...' 
when parsing: SELECT col1, col2 FROM [Firstmodel] 
JOIN [model2] AS [a] ON col1 = col5 
JOIN [] JOIN [] JOIN [] JOIN [] WHERE col3 = '2' ORDER BY col1 (180)

Is there a alternate way to join tables without creating models?

Was it helpful?

Solution

You have to show from which model col1 col5 etc are coming. Try rewriting your query by this example:

$builder->from('Robots')
    ->join('RobotsParts', 'Robots.id = RobotsParts.robots_id', 'p')
    ->join('Parts', 'Parts.id = RobotsParts.parts_id', 't');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top