Add base_table to entity query metadata

Created on 15 September 2022, over 2 years ago
Updated 30 January 2023, almost 2 years ago

Problem/Motivation

I believe there is a bug in node_query_node_access_alter() when node entity query is used with a condition on the entity reference field when accessCheck(TRUE) and at least one node grant exist.

Because Query::prepare() has required info , but doesn't set 'base_table' in metadata , node_query_node_access_alter() doing it's guess work, specifically here:

foreach ($tables as $table_info) {
      if (!($table_info instanceof SelectInterface)) {
        $table = $table_info['table'];
        // Ensure that 'node' and 'node_field_data' are always preferred over
        // 'node_revision' and 'node_field_revision'.
        if ($table == 'node' || $table == 'node_field_data') {
          $base_table = $table;
          break;
        }
        // If one of the node base tables are in the query, add it to the list
        // of possible base tables to join against.
        if (in_array($table, $node_base_tables)) {
          $base_table = $table;
        }
      }
    }

in describe scenario $tables would have two tables:

  1. base_table - node_revision
  2. field table, like node_revision__field_reference

with both present in $node_base_tables the last one would be used.

Steps to reproduce

$query = $entity_type_storage->getQuery();
$query->allRevisions();
$query->condition("$field_name.target_id", $entity->id());
$ids = $query->execute();

Expected

What is observed currently is :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'node_revision__field_reference.nid' in 'where clause': SELECT "base_table"."vid" AS "vid", "base_table"."nid" AS "nid"
FROM "node_revision" "base_table"
LEFT JOIN "node_revision__field_reference" "node_revision__field_reference" ON "node_revision__field_reference"."revision_id" = "base_table"."vid"
WHERE ("node_revision__field_reference"."field_dm_tender_results_target_id" = :db_condition_placeholder_0) OR (EXISTS (SELECT "na"."nid" AS "nid"
FROM
"node_access" "na"
WHERE ((("gid" IN (:db_condition_placeholder_1)) AND ("realm" = :db_condition_placeholder_2)) OR (("gid" IN (:db_condition_placeholder_3)) AND ("realm" = :db_condition_placeholder_4)) OR (("gid" IN (:db_condition_placeholder_5)) AND ("realm" = :db_condition_placeholder_6)) OR (("gid" IN (:db_condition_placeholder_7)) AND ("realm" = :db_condition_placeholder_8))) AND ("na"."grant_view" >= :db_condition_placeholder_9) AND (("node_revision__field_reference"."nid" = "na"."nid"))));

the issue is clearly in the last condition - "node_revision__field_reference"."nid" = "na"."nid" , which should be "node_revision"."nid" = "na"."nid"

Proposed resolution

Add the test covering scenario from description.

Based on the testing outcome figure out what needs fixing.

Remaining tasks

Write a test - see #2649268-24: Entity Field Query throws QueryException when querying all revisions and setting condition on entity reference field where target doesn't support revisions for example.

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

10.1

Component
Entity 

Last updated 2 days ago

Created by

🇳🇿New Zealand RoSk0 Wellington

Live updates comments and jobs are added and updated live.
  • Needs subsystem maintainer review

    It is used to alert the maintainer(s) of a particular core subsystem that an issue significantly impacts their subsystem, and their signoff is needed (see the governance policy draft for more information). Also, if you use this tag, make sure the issue component is set to the correct subsystem. If an issue significantly impacts more than one subsystem, use needs framework manager review instead.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Production build 0.71.5 2024