EntityFieldQuery::fieldCondition

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

Adds a condition on field values.

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 659

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

A little more explanation on delta_groups

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

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

A little more explanation on columns

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

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();

Another example using taxonomy term reference

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();
?>

Additional Info

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 about NULL values?

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

Null Values conditions...

These worked for me:

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

and use this for nid reference:

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

But what about fields with no value at all?

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.

Still got errors

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

I have an issue with 100000 value

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

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, '=');

EntityFieldQuery not working in bootstrap

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?

Solution

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.

fieldCondition between not working

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');

Nodes without a specific field

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

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();

Login or register to post comments