2

I have a content type called foo and it has a textfield called field_bar. I have one that has a value set to baz.

I'd like to use the EntityFieldQuery object to find all foos that have field_bar with the baz value.

$nodeQuery = new EntityFieldQuery();
  $nodeQuery->entityCondition('entity_type', 'node', '=')
    ->entityCondition('bundle', 'foo') 
    ->fieldCondition('field_bar', 'value', '12345', '=');

The problem is that I get no results or error back. If I remove the fieldCondition, I get all foos including the one I am looking for.

As the documentation is vague and only for experts, I googled a lot, but can't find any reason for my query not returning my node.

I also tried with

->fieldCondition('field_bar', 'value', array('12345'), '=');

to see if that matters, but its the same. I dd-ed the $nodeQuery but can't see the executed SQL.

Is there anything special with textfields?

3
  • I am in the context of a REST service resource operation, but as w/o the fieldcondition I see all nodes, I guess its not about permissions..
    – jabal
    Commented Mar 1, 2014 at 13:22
  • Are you sure your db values are actually what you expect? Does ->fieldCondition('field_bar', 'value', '%12345%', 'LIKE'); give you the right results, for example?
    – Clive
    Commented Mar 1, 2014 at 13:57
  • @Clive: thx, no result with % and like. Double checked that the match is exact anyways, no whitespace around the value.
    – jabal
    Commented Mar 1, 2014 at 18:18

3 Answers 3

2

From the canonical reference on EFQs,

->fieldCondition($field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL)

These conditions are specific to a field implementation.

Looking at the body field in the article node type, a field condition would look like this: ->fieldCondition('body', 'value', 'A', 'STARTS_WITH')

  • field name. Though the field table in the database is named 'field_data_body', the actual field name is 'body'. This is in the field_config_instance table.

  • column. This is the column in the database that should be matched, with field name prefix removed. For body field, the database columns are: body_value, body_summary, body_format, and language. 'value', 'summary', 'format', and 'language' are the actual arguments you would use. Likewise an image field would use 'fid', 'alt', and 'title' as column names; an entity reference field would use 'target_id' and 'target_type' as column names.

So, your syntax is correct, assuming your field name is correct (you want the machiine name), and your column is correct. Text and Longtext fields both use "value". You can verify this by looking at the database itself.

If your field name is field_bar, there will be a table called field_data_field_foo. Look at the columns. You should see one called field_bar_value.

1

Ok, this is very important!

The solution was adding this to the query:

 $nodeQuery->addMetaData('account', user_load(1));

As I mentioned I was running the code in the context of a Services module resource request. Looks like that I am prevented from filtering on a particular field, however not prevented from getting everything if I don't specify filter just load all and use node_load.

2
  • EFQ automatically enforces node_access when you query against node entities. By adding that account, you are essentially bypassing all node access permissions (ie, you are introducing a security issue into your site). The node that you are trying to get likely isn't accessible by anonymous users, either because it is unpublished or because of another access restriction.
    – mpdonadio
    Commented Mar 1, 2014 at 19:55
  • you can bypass this need: drupal.stackexchange.com/a/39548/13366 Commented Mar 2, 2014 at 5:19
0

Have you tried to debug that EntityFieldQuery with dpm() like this?

dpm($nodeQuery->execute());

What is your result?

PS: you need the Devel module to run dpm() function...

1
  • I dumped the query, actually its a huge structure, I see my condition exploded there, nothing special. The raw SQL is not shown.
    – jabal
    Commented Mar 1, 2014 at 18:19

Your Answer

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

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