public function EntityFieldQuery::fieldCondition

You are here

7 entity.inc public EntityFieldQuery::fieldCondition($field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL)

Adds a condition on field values.

Note that entities with empty field values will be excluded from the EntityFieldQuery results when using this method.

Parameters

$field: Either a field name or a field array.

$column: The column that should hold the value to be matched.

$value: The value to test the column value against.

$operator: The operator to be used to test the given value.

$delta_group: An arbitrary identifier: conditions in the same group must have the same $delta_group.

$language_group: An arbitrary identifier: conditions in the same group must have the same $language_group.

Return value

EntityFieldQuery The called object.

See also

EntityFieldQuery::addFieldCondition

EntityFieldQuery::deleted

File

includes/entity.inc, line 657

Class

EntityFieldQuery
Retrieves entities matching a given set of conditions.

Code

public function fieldCondition($field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
  return $this->addFieldCondition($this->fieldConditions, $field, $column, $value, $operator, $delta_group, $language_group);
}

Comments

The $delta_group parameter allows you to group conditions together. I have always read the docs to mean that we were specifying the delta within the field itself, but this is not the case.

For example, if you want to select entities based on two columns from the same field, you use a delta group to make sure that the conditions are dependent.

For example, to select users for an organic group with a specific state, I have to add the $delta_group (0) to the fieldCondition. Without it there is nothing to connect the group and the state.

$query
    ->entityCondition('entity_type', 'user')
    ->fieldCondition('group_audience', 'gid', $gid, '=', 0)
     ->fieldCondition('group_audience', 'state', (array) $states, 'IN', 0);

Thanks for this hint when querying against OG entities! Great tip.

I had a little trouble finding the right $column to use, here is a little breakdown of what I learned, hope it helps!

  1. When you create the field entitled field_yourfield drupal creates the mysql table field_data_field_yourfield
  2. Depending on what type of field you created, will see different information stored in this table but this table's column is what you will feed in for $column
    NOTE: the name of your field might preface some of the columns such as "field_yourfield_var" to use this column simply enter "var"

Hope this helps future googlers

Just to make it more clear I'll put an example.
If you have a field named 'field_city', the table will be 'field_data_field_city'.
In this table will be several columns. As column you have to write the name of the column as is in the db, except for those that begin by the name of the field. If you have a column named 'field_city_value' you just need to write 'value'. If you have a column named 'deleted' you have to write 'deleted'.

Example:

$result = $query
  ->entityCondition('entity_type', 'node')
  ->propertyCondition('status', 1)
  ->fieldCondition('field_city', 'value', 'Barcelona', '=')
  ->execute();

While fields such as text use '_value' other times you are going to want to check the field's schema for specific column name.

To do taxonomy term reference lookup using EntityFieldQuery you will want to reference the column field_TITLE_tid as 'tid'. In the example below, the taxonomy term reference field I am using is called 'foo' and I am querying the table 'field_data_field_foo' for the column 'filed_foo_tid'.

<?php
  $query
= new EntityFieldQuery();
 
$query
   
->entityCondition('entity_type', 'node')
    ->
entityCondition('bundle', 'article')
    ->
propertyCondition('status', 1)
    ->
propertyOrderBy('created', 'DESC')
    ->
fieldCondition('field_foo', 'tid', array('1', '2'))
    ->
range(0,10);
 
$result = $query->execute();
?>

You can also find what 'column' to use by looking at field_info_fields() command, which produces a keyed array: keys being the field_name, and value being the properties of that field. Looking at the property, you will see another key called 'columns'. I observe that the first value is usually what the 'column' to use, which is usually 'value', but for different field types it could be different -- such as tid for taxonomy, fid for images and files, etc.

It would be helpful to dig in the code of field api to see how drupal determines exactly which column to use.

What is the correct way to select the entities that have fields with NULL value?

These worked for me:

->fieldCondition('field_name', 'value', 'NULL', '!=');

and use this for nid reference:

->fieldCondition('field_name_ref', 'nid', 'NULL', '!=');

While this works for fields that have a null value in the database, what about fields that simply have no value at all (null or otherwise)? This can arise from fields that have no default value or were added to existing content. These rows don't exist in the database, and it doesn't seem possible to query for these entities.

I was looking into the same problem, but many answers told me this couldn't be done.
That's when i noticed this sentence in the above explanation:

Note that entities with empty field values will be excluded from the EntityFieldQuery results when using this method.

So this means by adding a field_condition, you are effectively removing all entities that have no entry for this field.

So i'm guessing that (for now ?) this isn't possible.

It would seem like an interesting case to add though.

I'm having the exact same issue. Seems it would be a pretty common use case: 1) Live site with lots of nodes being added. 2) Enhance site, add a field. 3) Build a query based on the value of that field.

EFQ might actually just be the wrong thing to use in this situation. Writing your own SQL to make the appropriate JOINs might be the way to go.

I had to go this way to avoid errors on some plateforms
->fieldCondition('field_name', 'value', NULL, 'IS NOT NULL');

I have a form with DDL that used to filter some search result.
fieldCondition works well for all other value but not for ''100000''.
here is my code :

$query = new EntityFieldQuery();
$entities = $query ->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'usedvehicle')
->propertyCondition('status', 1, '=')
->propertyCondition('language', $language->language, '=');

...

// Price
if ($pricef == 'all' AND $pricet != 'all')  {
$query->fieldCondition('oeo_vehicleprice_internet', 'value', $pricet, '<=');
}
else if ($pricef != 'all' AND $pricet == 'all') {
$query->fieldCondition('oeo_vehicleprice_internet', 'value', $pricef, '>=');
}
else if ($pricef != 'all' AND $pricet != 'all') {
$query->fieldCondition('oeo_vehicleprice_internet', 'value', array($pricef, $pricet), 'between');
}
else {

}

Why if i select the value "100000" as pricet, the query return nothing. with 90000 it works, So is this compare value as text by character instead of integer ? Thank you for help

A problem with fieldCondition.

When I add a fieldCondition like below, I get an empty result. I tried with different operators and different fields. (If i misspell field I get an error, so the field name is ok). I can't find what is wrong. If I comment the line with fieldCondition I get the results with field_profile_age and exact value inside.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'profile')
->propertyCondition('status', 1)
->fieldCondition('field_profile_age', 'value', 30, '=');

The problem in previous post happens to me if I'm doing an EntityFieldQuery in bootstrap.
My code before calling EFQ is:

// drupal bootstrap
$drupal_directory = "../drupal_folder";
$current_directory = getcwd();
chdir($drupal_directory);
define('DRUPAL_ROOT', getcwd());
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Am I doing something wrong with this or is there a bug in EFQ when bootstrapping?

You need to add:
->addMetaData('account', user_load(1)) //or any other authenticated user
in your EntityFieldQuery to run the query with the right permissions.

This helped me solve my problem and kept me from beating my head against the wall. Thanks alesr!!

hi
Any one help me i am using the between on query but it's not working
see this code.
$query->fieldCondition('field_amountpaid', 'value', array('501','5000'),'BETWEEN');

Does anyone has any idea how to create a query on non-existing fields?

Like if I want to get all nodes without a specific field.

For example: 10 nodes, 4 of them with a field called "field_counter", 6 without that field ...

How do I get the 6 nodes without the field "field_counter"?

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
      ->fieldCondition('field_counter', ?????);

I imagine the bundle would define if it has a field or not. So if your node type 'with_field' had the field_counter, and your node type 'without_field' did not, your query would look something like

$query= new EntityFieldQuery();
$result = $query
                  ->entityCondition('entity_type', 'node')
                  ->entityCondition('bundle', 'without_field')
                  ->execute();

I actually work on a Drupal project, with several nodes existing. And I create a module which add a field to all this nodes. So the bundle doesn't define if it has a field or not.
And so, i've got the same problem as hekele or tirithen. I want to get all nodes that don't have this specific added field, just to set it with a default value in a bulk operation for example..

PS : sorry for my english, i don't speak very well (french guy...)

I'm using the Entity reference module and want to select all nodes that has no parent defined. I'm trying with:

$query = new EntityFieldQuery();
$children = $query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'page')
->propertyCondition('status', 1)
->fieldCondition('field_parent', 'target_id', null, 'IS NULL')
->execute();

.. but that does not give anything as the field "field_parent" is missing and not set to NULL. If I use IS NOT NULL I get all nodes that has a parent.

How can I select all nodes that has no parent (or is missing the field "field_parent")?

agreed, re: between operation for the fieldCondition not working.

I have a field which acts as a filter.

I want to select all entities where the field matches the beginning of a string i provide to the request. Im fully aware that "startswith" is a function BUT it seems
ONLY give possibility to check if the FIELD starts with the $VARIABLE and not
that the $VARIABLE startswith FIELD

Anyone?

How should I create a query that lists the terms children?

  $query = new EntityFieldQuery() ;
  $query ->entityCondition('entity_type', 'taxonomy_term');
  $query ->entityCondition('bundle', TERM_MACHINE_NAME);
  $query ->propertyCondition('parent',0,'='); // error

can anyone tell me why this query failed :

<?php
$query
->entityCondition('entity_type', 'node')
        ->
entityCondition('bundle', 'my_type')
        ->
fieldCondition('field_date_operation', 'value', 'DATE_ADD(CURRENT_DATE,INTERVAL -1 DAY)', '=')
        ->
propertyCondition('status', 1);
?>

'DATE_ADD(CURRENT_DATE,INTERVAL -1 DAY)' is not supported in fieldCondition

Thakns in advance

Does anyone know how to use this to query on a field that has multiple values? My field_users is an Entity Reference field that allows multiple values. My current code isn't working:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'my_node_type', '=')
->propertyCondition('status', 1)
->fieldCondition('field_users', 'target_id', $user->uid, '=');

Nevermind - the query works. It was a permissions issue.

Helpfully documented with the internal function at EntityFieldQuery::addFieldCondition, I duplicate them here for our convenience:

$operator:
Possible values:

  • '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS': These
    operators expect $value to be a literal of the same type as the
    column.
  • 'IN', 'NOT IN': These operators expect $value to be an array of
    literals of the same type as the column.
  • 'BETWEEN': This operator expects $value to be an array of two literals
    of the same type as the column.

The operator can be omitted, and will default to 'IN' if the value is an
array, or to '=' otherwise.