3

I am retrieving a tid of a taxonomy term by using EntityFieldQuery. However, I need to cross-link the taxonomy_term_hierarchy table to list those terms which have a particular parent, but I am not able to get it. In the code below

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'taxonomy_term')
      ->propertyCondition('vid', $vocabulary)
      ->propertyCondition('name', $termino)
      ->propertyCondition('parent', $parent_id)

the last line is throwing the following (expected) error. The same code, without the last property condition, works correctly. How can I achieve the result?

The returned error is:

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

1
  • 2
    Just in case anyone is interested in the answer for Drupal 8, the condition would look like: $query->condition('parent.target_id', $parent_id); or if you want to query entities referencing taxonomy terms by a given parent id $query->condition('field_term_reference.entity.parent.target_id', $parent_id);
    – LarS
    Jun 17, 2019 at 2:31

1 Answer 1

4

Using only the EntityFieldQuery class, you cannot because the information about the taxonomy hierarchy is not contained in the database table used for the entity data, but in the taxonomy_term_hierarchy table.

You would need to first get all the taxonomy term IDs having $parent_id as parent, from that table, and then use the EntityFieldQuery class for the other part of the query. Truly, the EntityFieldQuery class is excessive, in this case, since its purpose is to make a query involving the values of field attached to an entity. It also slower than a normal query done with db_select(), and you would not get any benefit, in this case.

1
  • I had convinced myself that EntityFieldQuery was more abstract than db_select, avoiding potential problems when changing field names or the like. For your reply and other info it seems very limitated, and in Drupal 8 this limitation may change. I will use a simple db_query as you suggest.
    – Cesar
    Nov 18, 2015 at 17:08

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.