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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

plach created an issue. See original summary.

plach’s picture

Status: Active » Needs review
FileSize
765 bytes
plach’s picture

Here's a D7 patch.

Status: Needs review » Needs work

The last submitted patch, 3: drupal-db_join_condition-2582069-3.patch, failed testing.

plach’s picture

Version: 7.x-dev » 8.0.x-dev
Issue summary: View changes
Status: Needs work » Needs review

Back to 8.0.x

marco’s picture

Status: Needs review » Reviewed & tested by the community

#3 (D7) reviewed and tested, works well.

plach’s picture

Issue tags: +Needs tests

Thanks, actually I realized we need additional test coverage.

plach’s picture

Status: Reviewed & tested by the community » Needs work
Nephele’s picture

Status: Needs work » Closed (duplicate)

This 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.

plach’s picture

Version: 8.0.x-dev » 7.x-dev
Category: Bug report » Feature request
Status: Closed (duplicate) » Needs review
Parent issue: » #2275519: Unable to use Condition objects with joins

In #2275519-15: Unable to use Condition objects with joins, @alexpott suggested to open a separate issue for the D7 backport, so reopening this.

plach’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests
plach’s picture

Title: Passing a Condition object as join condition makes the query break due to syntax error » [D7] Unable to use Condition objects with joins
Status: Needs work » Needs review
Issue tags: -Needs backport to D7
FileSize
2.41 KB
5.26 KB

Here's the backport.

The last submitted patch, 12: drupal-db_join_condition-2582069-12.test.patch, failed testing. View results

marco’s picture

Status: Needs review » Reviewed & tested by the community

#12 looks good and works well

plach’s picture

Issue summary: View changes

The last submitted patch, 16: 2582069-16_test-only.patch, failed testing. View results

mcdruid’s picture

Issue tags: +RTBM

Patch looks good, it seems like a straight backport of the D8 issue and the code is still used in D9.

+1

  • poker10 committed 11082e2 on 7.x
    Issue #2582069 by plach, poker10, Nephele: [D7] Unable to use Condition...
poker10’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -RTBM

Thanks all!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.