Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
This code does not work:
$query = db_select('table_a', 'a');
$join_condition = db_and();
foreach ($fields as $field) {
$field = db_escape_field($field);
$join_condition->where("a.$field = b.$field");
}
$query->innerJoin('table_b', 'b', $join_condition);
The reason is that the condition is not compiled, and thus converting the query to string breaks.
Proposed resolution
Backport the D8 fix committed in #2275519: Unable to use Condition objects with joins.
Remaining tasks
Reviews
User interface changes
None
API changes
None
Data model changes
None
Comments
Comment #2
plachComment #3
plachHere's a D7 patch.
Comment #5
plachBack to 8.0.x
Comment #6
marco CreditAttribution: marco at Tag1 Consulting commented#3 (D7) reviewed and tested, works well.
Comment #7
plachThanks, actually I realized we need additional test coverage.
Comment #8
plachComment #9
Nephele CreditAttribution: Nephele commentedThis is a duplicate of #2275519: Unable to use Condition objects with joins -- where the issue was deemed to be a feature request, not a bug.
Comment #10
plachIn #2275519-15: Unable to use Condition objects with joins, @alexpott suggested to open a separate issue for the D7 backport, so reopening this.
Comment #11
plachWe should backport #2275519-10: Unable to use Condition objects with joins.
Comment #12
plachHere's the backport.
Comment #14
marco CreditAttribution: marco at Tag1 Consulting commented#12 looks good and works well
Comment #15
plachComment #16
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commentedRerolling the patch #12, for some reason it does not applied anymore. Patch looks good, it seems like a straight backport of the D8 issue and the code is still used in D9.
Comment #18
mcdruid+1
Comment #20
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commentedThanks all!