3

I have the following code:

$query = new EntityFieldQuery();

$result = $query
    ->entityCondition('entity_type', 'field_collection_item', '=')
    ->propertyCondition('field_name', 'field_version', '=')
    ->fieldCondition('field_version', 'entity_id', '105', '=')
    ->execute()
;

The first two conditions are fine, but the fieldCondition() is causing the following SQL exception:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_data_field_version0.field_version_entity_id' in 'where clause'...

The problem seems to be that 'field_version' is prepended to the column 'entity_id'. I have looked inside _field_sql_storage_query_field_conditions() and there appears to be no way to stop it from prepending this. This is the table I am trying to perform the fieldCondition on:

+-------------+---------------+---------+-----------+-------------+----------+-------+---------------------+    
| entity_type | bundle        | deleted | entity_id | revision_id | language | delta | field_version_value |    
+-------------+---------------+---------+-----------+-------------+----------+-------+---------------------+
| node        | shopping_cart |       0 |       105 |         123 | und      | 0     |                   5 |
| node        | shopping_cart |       0 |       108 |         126 | und      | 0     |                   8 |
+-------------+---------------+---------+-----------+-------------+----------+-------+---------------------+

How may I perform this query correctly?

3 Answers 3

2

entity_id is the entity ID of the entity the field is attached to; in this case, the ID of your field collection item. To add a condition on that, you need to use a propertyCondition() on the "field_collection_item" primary key.

1
  • I'm not sure I follow. I have added this to the code block above: ->propertyCondition('entity_id', '105', '=') but this gives me Unknown column 'field_collection_item.entity_id' in 'where clause. In other words, it's trying to run the condition on the field_collection_item table when I want to be running the clause on the field_data_field_version table. Please could you expand?
    – Pete171
    Apr 23, 2012 at 12:37
1

I managed to solve this a different way.

I was trying to retrieve the entity ID in order to retrieve the information from the node that the field collection was attached to. Instead, however, I used node_load() to load the relevant nodes. I then was able to retrieve the entity ID of the field collection object and load it using entity_load().

1
  • 3
    If that's what you want, then this is the correct way and EntityField does not help you. What EntityFieldQuery does is load entity ids of entities based on their properties and fields. You could for example use it if you want to load the node(s) that has a specific version value by first loading the field collection item and then loading the node that references the field collection item. You're doing the opposite, you have the node and want the version value. To get field values, you always need to load the entities.
    – Berdir
    Apr 23, 2012 at 17:03
1

For anyone still intrested in this one, it can be done via:

->entityCondition('entity_id', '68', '!=')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.