Search code examples
joinkohanaquery-builderkohana-3.3

Kohana 3. Join with multiple condition using OR


I'm using Kohana Query Builder and trying do next:

$query
 ->join(«t1», «INNER»)
 ->on(«t1.id»,»=»,»t2.parent_id»)
 ->on(«t1.id»,»=»,»t3.x_id»)

This means:

INNER JOIN t1
ON(t1.id = t2.parent_id AND t1.id = t3.x_id)

But how force to use OR instead of AND of KO3 query builder join methods?

INNER JOIN t1
ON(t1.id = t2.parent_id OR t1.id = t3.x_id)

Solution

  • Due to Konaha source code all on() methods are concatinated with AND:

    // Concat the conditions "... AND ..."
    $sql .= ' ON ('.implode(' AND ', $conditions).')';
    

    So you have at least 2 ways:

    • Redifine Database_Query_Builder_Join methods to support OR concatinator
    • (it's cheating) Use DB::expr() as 3rd parameter to on() method like:

      ->on('t1.id', '=', DB::expr('t2.parent_id OR t1.id = t3.x_id))