From 364fd1776e5766055061d4c0faa877fd0d95b9ea Mon Sep 17 00:00:00 2001
From: Dries <dries@buytaert.net>
Date: Sun, 15 Jul 2012 06:24:51 -0700
Subject: [PATCH] - Patch #1325628 by damien_vancouver, xjm, Damien Tournoud,
 TravisCarden: fixed EntityFieldQuery::propertyQuery missing fully qualified
 column names causes ambiguous column DB error.

---
 .../lib/Drupal/entity/EntityFieldQuery.php    | 16 +++++++++++-----
 .../entity/Tests/EntityFieldQueryTest.php     | 19 +++++++++++++++++++
 .../modules/field_test/field_test.module      | 11 +++++++++++
 3 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/core/modules/entity/lib/Drupal/entity/EntityFieldQuery.php b/core/modules/entity/lib/Drupal/entity/EntityFieldQuery.php
index cf5115621c69..c5c96303dc6f 100644
--- a/core/modules/entity/lib/Drupal/entity/EntityFieldQuery.php
+++ b/core/modules/entity/lib/Drupal/entity/EntityFieldQuery.php
@@ -809,7 +809,7 @@ protected function propertyQuery() {
     $select_query->addExpression(':entity_type', 'entity_type', array(':entity_type' => $entity_type));
     // Process the property conditions.
     foreach ($this->propertyConditions as $property_condition) {
-      $this->addCondition($select_query, "$base_table." . $property_condition['column'], $property_condition);
+      $this->addCondition($select_query, $base_table . '.' . $property_condition['column'], $property_condition);
     }
     // Process the four possible entity condition.
     // The id field is always present in entity keys.
@@ -817,7 +817,7 @@ protected function propertyQuery() {
     $id_map['entity_id'] = $sql_field;
     $select_query->addField($base_table, $sql_field, 'entity_id');
     if (isset($this->entityConditions['entity_id'])) {
-      $this->addCondition($select_query, $sql_field, $this->entityConditions['entity_id']);
+      $this->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['entity_id']);
     }
 
     // If there is a revision key defined, use it.
@@ -825,7 +825,7 @@ protected function propertyQuery() {
       $sql_field = $entity_info['entity keys']['revision'];
       $select_query->addField($base_table, $sql_field, 'revision_id');
       if (isset($this->entityConditions['revision_id'])) {
-        $this->addCondition($select_query, $sql_field, $this->entityConditions['revision_id']);
+        $this->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['revision_id']);
       }
     }
     else {
@@ -850,7 +850,13 @@ protected function propertyQuery() {
     }
     $id_map['bundle'] = $sql_field;
     if (isset($this->entityConditions['bundle'])) {
-      $this->addCondition($select_query, $sql_field, $this->entityConditions['bundle'], $having);
+      if (!empty($entity_info['entity keys']['bundle'])) {
+        $this->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['bundle'], $having);
+      }
+      else {
+        // This entity has no bundle, so invalidate the query.
+        $select_query->where('1 = 0');
+      }
     }
 
     // Order the query.
@@ -863,7 +869,7 @@ protected function propertyQuery() {
         $select_query->orderBy($id_map[$key], $order['direction']);
       }
       elseif ($order['type'] == 'property') {
-        $select_query->orderBy("$base_table." . $order['specifier'], $order['direction']);
+        $select_query->orderBy($base_table . '.' . $order['specifier'], $order['direction']);
       }
     }
 
diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityFieldQueryTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityFieldQueryTest.php
index 81bc286d980e..fd609d8eb297 100644
--- a/core/modules/entity/lib/Drupal/entity/Tests/EntityFieldQueryTest.php
+++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityFieldQueryTest.php
@@ -1583,4 +1583,23 @@ function assertEntityFieldQuery($query, $intended_results, $message, $ordered =
       $this->fail('Exception thrown: '. $e->getMessage());
     }
   }
+
+  /**
+   * Tests EFQ table prefixing with multiple conditions and an altered join.
+   *
+   * @see field_test_query_efq_table_prefixing_test_alter()
+   */
+  function testTablePrefixing() {
+    $query = new EntityFieldQuery();
+    $query = $query
+      ->entityCondition('entity_type', 'test_entity')
+      ->entityCondition('bundle', 'test_bundle')
+      ->entityCondition('entity_id', '1')
+      ->addTag('efq_table_prefixing_test');
+
+    $expected = array(array('test_entity', 1));
+
+    $this->assertEntityFieldQuery($query, $expected, 'An EntityFieldQuery returns the expected results when altered with an additional join on the base table.');
+  }
+
 }
diff --git a/core/modules/field/tests/modules/field_test/field_test.module b/core/modules/field/tests/modules/field_test/field_test.module
index 9b4a3aa71b3b..6821c97deca1 100644
--- a/core/modules/field/tests/modules/field_test/field_test.module
+++ b/core/modules/field/tests/modules/field_test/field_test.module
@@ -264,3 +264,14 @@ function field_test_field_widget_form_alter(&$element, &$form_state, $context) {
       break;
   }
 }
+
+/**
+ * Implements hook_query_TAG_alter() for tag 'efq_table_prefixing_test'.
+ *
+ * @see Drupal\entity\Tests\EntityFieldQueryTest::testTablePrefixing()
+ */
+function field_test_query_efq_table_prefixing_test_alter(&$query) {
+  // Add an additional join onto the entity base table. This will cause an
+  // exception if the EFQ does not properly prefix the base table.
+  $query->join('test_entity','te2','%alias.ftid = test_entity.ftid');
+}
-- 
GitLab