3

I have the following query which works:

$query = "SELECT field_code_description_value FROM {field_data_field_code_description} WHERE bundle = :vocabulary AND entity_id in (SELECT entity_id FROM {taxonomy_term_data} WHERE name = :name)";
  $results = db_query($query, array(':name' => $fund_code, ':vocabulary'=>'fund_code'));
  $terms = $results->fetchObject();

I'm trying to convert this into using EntityFieldQuery, as it seems to be the best way to go (and I need some practice), but I couldn't get it right. This is what I tried:

$query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type','taxonomy_term')
      ->entityCondition('bundle','fund_code')
      ->fieldCondition('taxonomy_term_data','field_code_description', $fund_code, '=')
      //->propertyCondition('vid', $fundCodeTaxonomy->vid);

    $results = $query->execute();
    foreach($results['taxonomy_term'] as $tid) {
      $term = taxonomy_term_load($tid);
      dpm($term);
    }

How do I properly use EntityFieldQuery here?

6

The code_description field does not appear in the WHERE condition of the original query. Therefore there is no need to add a fieldCondition on the EntityFieldQuery neither.

$query = new EntityFieldQuery();
$query
  ->entityCondition('entity_type', 'taxonomy_term')
  ->entityCondition('bundle', 'fund_code')
  ->propertyCondition('name', $fund_code) ;

$results = $query->execute();
foreach($results['taxonomy_term'] as $tid) {
  $term = taxonomy_term_load($tid);
  dpm($term);
  $items = field_get_items('taxonomy_term', $term, 'code_description');
  dpm($items);
}

However there is already the core function taxonomy_get_term_by_name doing exactly what you need here:

$terms = taxonomy_get_term_by_name($fund_code, 'fund_code');
foreach ($terms as $term) {
  dpm($term);
  $items = field_get_items('taxonomy_term', $term, 'code_description');
  dpm($items);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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