Problem/Motivation

If an entity query is run against a standard, non-DER, entity reference field, something is still trying to attach the _int logic to the query.

This query, for instance, given a base field called normal_reference:

    $result = \Drupal::entityTypeManager()->getStorage('entity_test_mul')->getQuery()
      ->condition('normal_reference.entity:user.status', 1)
      ->execute();

produces the following error:

Column not found: 1054 Unknown column 'entity_test_mul_property_data.normal_reference_int' in 'on clause': SELECT base_table.id AS id, base_table.id AS base_table_id
FROM
{entity_test_mul} base_table
INNER JOIN {entity_test_mul_property_data} entity_test_mul_property_data ON entity_test_mul_property_data.id = base_table.id
LEFT OUTER JOIN {users} users ON users.uid = entity_test_mul_property_data.normal_reference_int
INNER JOIN {users_field_data} users_field_data ON users_field_data.uid = users.uid
WHERE users_field_data.status = :db_condition_placeholder_0; Array
(
    [:db_condition_placeholder_0] => 1
)

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

jhedstrom created an issue. See original summary.

jhedstrom’s picture

Status: Active » Needs review
StatusFileSize
new2.71 KB

This demonstrates the issue. If the same test is run w/o DER enabled, this passes.

Status: Needs review » Needs work

The last submitted patch, 2: 2908109-02-TEST-ONLY.patch, failed testing. View results

jhedstrom’s picture

  /**
   * {@inheritdoc}
   */
  protected function addNextBaseTable(EntityType $entity_type, $table, $sql_column) {
    // Parent method is overridden in order to choose the correct column to
    // join on (string or int).
    $entity_type_id_key = $entity_type->getKey('id');
    if ($entity_type_id_key !== FALSE) {
      if (DynamicEntityReferenceItem::entityHasIntegerId($entity_type->id())) {
        $sql_column .= '_int';
      }
    }
    return parent::addNextBaseTable($entity_type, $table, $sql_column);
  }

This logic is appending _int for any entity reference field. Not sure how to fix as this is sitting bellow the field API level where we'd know whether this was a DER field or not...

jibran’s picture

WoW! I'd ping @Berdir to get some ideas around this.

jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new1.34 KB
new4.05 KB
new2.17 KB

Here's a demonstration of how we could resolve this with the attached patch for core. Not sure how readily we could get that in, so posting here for thoughts. This will fail on the test bots since they won't apply the core patch :)

The last submitted patch, 6: 2908109-06.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 6: 2908109-06-DO-NOT-TEST.patch, failed testing. View results

jibran’s picture

I think this makes sense let's create a core issue.

jhedstrom’s picture

Interestingly enough, this works in core 8.2.x. I'm currently running git bisect to figure out what changed.

I'll open up a core issue shortly.

jhedstrom’s picture

Ah, this is what changed between 8.2 and 8.4/8.5: #2424791: Entity query hardcodes entity_reference and entity specifier.

jhedstrom’s picture

jhedstrom’s picture

Ah, that's why it "works" in 8.2.x--the addNextBaseTable method didn't exist then, so the override we have here was never triggered.

jibran’s picture

Is it also broken for entity reference configurable fields?

jibran’s picture

StatusFileSize
new783 bytes
new3.48 KB
jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new2.42 KB
new5.13 KB
new5.89 KB

Is it also broken for entity reference configurable fields?

It appears to be. I've added a normal entity reference configurable field test here. It fails in the same manner, and passes with the proposed core fix.

The last submitted patch, 16: 2908109-16-TEST-ONLY.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 16: 2908109-16.patch, failed testing. View results

jibran’s picture

+++ b/src/Query/Tables.php
@@ -15,10 +15,11 @@ class Tables extends BaseTables {
+    $field_storage = func_get_arg(3);

We should add is_null check here. I think we should also fix for #2909425: Add the $field_storage parameter to Tables::addNextBaseTable at the same time. Add the fourth optional param if it's NULL then use func_get_arg(3) and if it is still NULL then don't add $field_storage check.

jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new1.29 KB
new6.32 KB

I made the entire conditional dependent on the field storage, since otherwise, this error will still be present on sites that somehow this method gets called for that aren't up-to-date and passing in the field storage...

Status: Needs review » Needs work

The last submitted patch, 20: 2908109-20.patch, failed testing. View results

jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new1.62 KB
new6.36 KB

This fixes some issues with the tests.

Status: Needs review » Needs work

The last submitted patch, 22: 2908109-22.patch, failed testing. View results

jibran’s picture

#2909425: Add the $field_storage parameter to Tables::addNextBaseTable is in so we have to fix it for both core version(8.4.x and 8.5.x) now.

jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new413 bytes
new6.54 KB

This should do it I think :)

  • jibran committed 46f7012 on 8.x-2.x authored by jhedstrom
    Issue #2908109 by jhedstrom, jibran: Entity queries broken for non-DER...
jibran’s picture

Status: Needs review » Fixed

Thanks, for the fix. Committed and pushed to 8.x-2.x. I'll create a new release soon.

Status: Fixed » Closed (fixed)

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