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
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
Comment #2
sergei_brill commentedNot sure the issue is related to the same problem. But adding it for now. May be it helps to understand the issue better.
Comment #5
clement.ferrier commentedComment #7
clement.ferrier commentedHello
I also tracked down this issue to
\Drupal\Core\Entity\Query\Sql\Tables::ensureFieldTableTo 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
Comment #11
smustgrave commentedThis 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.
Comment #18
clement.ferrier commentedRebased changes on
11.xand added a new test case for this issueComment #19
smustgrave commentedHello,
This appears to still need an issue summary update using the standard template