diff options
-rw-r--r-- | CHANGELOG.txt | 2 | ||||
-rw-r--r-- | includes/entity.inc | 16 | ||||
-rw-r--r-- | modules/field/tests/field_test.module | 11 | ||||
-rw-r--r-- | modules/simpletest/tests/entity_query.test | 19 |
4 files changed, 43 insertions, 5 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 69a6a4d19..3279d4b16 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -43,6 +43,8 @@ Drupal 7.15, xxxx-xx-xx (development version) structure change). - Changed the drupal_array_get_nested_value() function to return a reference (API addition). +- Fixed database errors due to ambiguous column names that occurred when + EntityFieldQuery was used in certain situations. Drupal 7.14 2012-05-02 ---------------------- diff --git a/includes/entity.inc b/includes/entity.inc index 96cc6e0ba..832abe2fd 100644 --- a/includes/entity.inc +++ b/includes/entity.inc @@ -1204,7 +1204,7 @@ class EntityFieldQuery { $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. @@ -1212,7 +1212,7 @@ class EntityFieldQuery { $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. @@ -1220,7 +1220,7 @@ class EntityFieldQuery { $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 { @@ -1245,7 +1245,13 @@ class EntityFieldQuery { } $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. @@ -1258,7 +1264,7 @@ class EntityFieldQuery { $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/modules/field/tests/field_test.module b/modules/field/tests/field_test.module index 0015cd905..37ea7b1dd 100644 --- a/modules/field/tests/field_test.module +++ b/modules/field/tests/field_test.module @@ -259,3 +259,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 EntityFieldQueryTestCase::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'); +} diff --git a/modules/simpletest/tests/entity_query.test b/modules/simpletest/tests/entity_query.test index 7a7c6222c..6d77c508b 100644 --- a/modules/simpletest/tests/entity_query.test +++ b/modules/simpletest/tests/entity_query.test @@ -1659,4 +1659,23 @@ class EntityFieldQueryTestCase extends DrupalWebTestCase { $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.'); + } + } |