Quantcast
Channel: Editor — DataTables forums
Viewing all articles
Browse latest Browse all 3740

Nested readonly `leftJoin` in `Join`

$
0
0

Just like a lot of others, I had the need of a leftJoin on a joined table. It turns out this is quite easy to do when both the joined table and the nested leftjoined table are readonly.

Implementation

All we have to do is copy the relevant leftJoin code form Editor.php to Join.php:
- the _leftJoin[] array
- the leftJoin($table, $field1, $operator, $field2) function to add a left join
- the private _perform_left_join($query) function
- Now, we need to append the join to the query statement in the public data function. The way to do this is to put

$this->_perform_left_join(stmt);

just before the call to $res = $stmt->exec();.

Usage

Now you can do something like

$editor = Editor::inst( $db, 'A', 'A.id')
$editor->Join(
    MJoin('B')
    ->link('B.id', 'A.b_id')
    ->leftJoin('C', 'C.id', '=', 'B.c_id')
    ->fields(
           Field::inst('B.id')->set(Field::SET_NONE),
           Field::inst('C.some_nice_property')->set(Field::SET_NONE)
     )
     ->set(Field::SET_NONE)
);

Make sure to use an alias for C if C is also leftjoined to A directly.

@allan, this might be a nice feature for future Editor versions as well, because I think this is quite useful, even without support for editing them.


Viewing all articles
Browse latest Browse all 3740