Problem

When 2 conditions on the same multiple field with different langcodes, it joins the field table only once.
Example (can be used in any controller)

$langcode = $this->languageManager()->getCurrentLanguage()->getId();
$defaultLangcode = $this->languageManager()->getDefaultLanguage()->getId();
$query = $this->entityTypeManager->getStorage('station')->getQuery();
$query->condition('status', 1);
$orCondition = $query->orConditionGroup();
$orCondition->condition('name', $q, 'CONTAINS', $langcode)
  ->condition('name', $q, 'CONTAINS', $defaultLangcode);
$query->condition($orCondition);
$stations = $query->execute();

Station is a custom entity type, name is a base field of type string with cardinality set to FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
The resulting query looks like

SELECT base_table.id AS id, base_table.id AS base_table_id
FROM 
{station} base_table
INNER JOIN {station_field_data} station_field_data ON station_field_data.id = base_table.id
LEFT JOIN {station__name} station__name ON station__name.entity_id = base_table.id AND station__name.langcode = :langcode0
WHERE (station_field_data.status = :db_condition_placeholder_1) AND ((station__name.name_value LIKE :db_condition_placeholder_2 ESCAPE '\\') or (station__name.name_value LIKE :db_condition_placeholder_3 ESCAPE '\\'))
GROUP BY base_table.id

But expected

SELECT base_table.id AS id, base_table.id AS base_table_id
FROM 
{station} base_table
INNER JOIN {station_field_data} station_field_data ON station_field_data.id = base_table.id
LEFT JOIN {station__name} station__name ON station__name.entity_id = base_table.id AND station__name.langcode = :langcode0
LEFT JOIN {station__name} station__name_1 ON station__name_1.entity_id = base_table.id AND station__name_1.langcode = :langcode1
WHERE (station_field_data.status = :db_condition_placeholder_1) AND ((station__name.name_value LIKE :db_condition_placeholder_2 ESCAPE '\\') or (station__name_1.name_value LIKE :db_condition_placeholder_3 ESCAPE '\\'))
GROUP BY base_table.id

So, when different langcodes are used for query, it doesn't join field table again using another value for condition with langcode.
As I see joining of field tables happens here \Drupal\Core\Entity\Query\Sql\Tables::ensureFieldTable. And here it doesn't take in account that langcode may be different.

Issue fork drupal-2941073

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

sergei_brill created an issue. See original summary.

sergei_brill’s picture

Not sure the issue is related to the same problem. But adding it for now. May be it helps to understand the issue better.

Version: 8.4.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

clement.ferrier made their first commit to this issue’s fork.

clement.ferrier’s picture

Version: 8.9.x-dev » 9.3.x-dev

clement.ferrier’s picture

Status: Active » Needs review

Hello

I also tracked down this issue to \Drupal\Core\Entity\Query\Sql\Tables::ensureFieldTable
To fix, the key used to distinguish already joined field table should reflect the different variation the join can have.

I implemented this in the MR!1342

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests, +Needs issue summary update

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

This will need a test case to show the issue.
Also needs an issue summary update using the standard template.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

clement.ferrier changed the visibility of the branch 2941073-entityquery-doesnt-support to hidden.

clement.ferrier changed the visibility of the branch 2941073-9.3.x to hidden.

clement.ferrier changed the visibility of the branch 2941073-entityquery-doesnt-support-OR-with-langcodes to hidden.

clement.ferrier’s picture

Status: Needs work » Needs review

Rebased changes on 11.x and added a new test case for this issue

smustgrave’s picture

Status: Needs review » Needs work

Hello,

This appears to still need an issue summary update using the standard template

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.