Nested readonly `leftJoin` in `Join`
Nested readonly `leftJoin` in `Join`
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.
Replies
Agreed. This is something I'd like to see in future versions of Editor as well.
Thanks for posting your workaround for now!
Allan
Hi RagnarGrootKoerkamp. I am trying to do exactly what you describe above but I must be making a mistake as I cannot get it to work. Is there any chance you could share your Join.php file please?
I get "DataTables warning: table id=product - Table selected fields (i.e. '{table}.{column}') in
Join
must have a name alias which does not contain a period ('.'). Use name('---') to set a name for the field"I have experienced this error before and never found a way to solve it. Replacing the period for --- doesn't work, if that is what it is suggesting?
Thanks
I hope this is still relevant:
This is probably due to using column names that are not prefixed with their table name in the
Field::inst('column_name')
calls. Instead, it should beField::inst('table_name.column_name')
.Hi,
Sorry for slow reply. I still cant get this to work and the issue is the prefix. Even without the nested leftjoin if I add a prefix to the field within my Mjoin instance it doesnt work.
This works fine,
This does not work,
I see the same error "DataTables warning: table id=product - Table selected fields (i.e. '{table}.{column}') in Join must have a name alias which does not contain a period ('.'). Use name('---') to set a name for the field".
I don't understand because that is exactly how I would initiate a field outside of an MJoin?
Thanks
Got it. I don't know why it didn't occur to me before but obviously I can just use
as
. This works fine.Thanks, I now have the leftJoin working within the Mjoin, this is a really great addition and hopefully this will become part of the core at somepoint because it is super useful.
Nice!
I think I never had this problem because I usually create fields by calling
which gives it the name
column
as far as Editor is concerned.