When specifying a views relationship, you can add extra conditions to the resulting join as such:
$data['my_table']['my_field']['relationship'] = array(
'id' => 'standard',
'base' => 'other_table',
'base field' => 'other_field',
'extra' => array(
array(
'field' => 'this',
'value' => 1,
),
array(
'field' => 'that',
'value' => array(3,4,5),
),
),
);
The above extra array should produce a join something like:
LEFT JOIN `other_table` ON `my_table`.`my_field` = `other_table`.`other_field` AND (`other_table`.`this` = 1 AND `other_table`.`that` IN (3, 4, 5))
However, currently it produces this:
LEFT JOIN `other_table` ON `my_table`.`my_field` = `other_table`.`other_field` AND (`other_table`.`this` = 1 AND `other_table`.`that` IN (1, 3, 4, 5))
Note that the argument from the first additional join condition is finding its way into the second additional join's list of arguments.
This is caused by core/modules/views/src/Plugin/views/join/JoinPluginBase.php on about line 257:
$placeholder = '( ' . implode(', ', array_keys($arguments)) . ' )';
The $arguments array is cumulative for all extra join conditions, meaning that any previous join conditions are imploded here for the current join condition. Instead, what is needed here is a local arguments list for the current join condition.
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | 2299215-09-views-join.patch | 3.32 KB | jhedstrom |
| #9 | interdiff.txt | 830 bytes | jhedstrom |
Comments
Comment #1
torrance123 commentedComment #2
torrance123 commentedComment #3
dawehnerI wonder whether we can write a test for that. Do you think you can do that?
Comment #4
jhedstromThis adds a test. Attached separately to show existing failure (and roled into the patch to show fix).
Comment #6
dawehnerNice!
Comment #7
jhedstromAlex Pott recommended on IRC that we should use
assertContains()here. However, that doesn't exist from what I can tell in SimpleTest, only in PHPUnit.Also, there was a question regarding the use of the
namefield twice, and I think it is simply testing that joins can occur with different conditions on the same field.Comment #8
alexpott@jhedstrom - sorry I should have checked that - I saw the name *UnitTest and assumed :(
The patch looks great apart from...
I don't really understand the assertion message.
Comment #9
jhedstromHow about this message?
Comment #10
jhedstromBouncing back to RTBC since the only change was wording on the message.
Comment #11
catchNew assertion message looks better. Committed/pushed to 8.0.x, thanks!