3

I have to query my content types (firm_unit) based on different fields conditions, and for that I came to use EntityFieldQuery.

$query = new EntityFieldQuery;

$query
  ->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', $type = 'firm_unit')
  ->propertyCondition('status', 1);

This is nice when I want to query conditions on direct content fields, like these:

$query->fieldCondition('field_name', 'value', $keyword, 'CONTAINS');
$query->fieldCondition('field_sector', 'value', $sector, '=');

It seems to be unable to directly access, and then query by, referenced contents, like names/titles and fields of referenced terms (it just can access their tids).

Which is then the way I may query my node contents based on term referenced values?

Should I query the tid before (is the _taxonomy_get_tid_from_term func the proper to use?), and then query the content like this?

$my_tid = _taxonomy_get_tid_from_term ($term_to_match);<br/>
$query->fieldCondition('term_fieldname', 'tid', $my_tid);

2 Answers 2

1

You should probably use the taxonomy_get_term_by_name() function.

$terms = taxonomy_get_term_by_name($your_term_name);
$tid = $terms[0]->tid; // Assuming you have one term with that name.

Your query would then contain:

$query->fieldCondition('term_fieldname', 'tid', $tid);

So basically your question contains the answer already.

1

EntityFieldQuery can not, as you have noticed JOIN tables, because it may run queries against a backend where JOIN does not make sense.

You will need bridge this gap in code, which is what your own suggestion is doing, it looks fine to me.

However, if you don't intend to run on MongoDB or some such, my personal preferance is would be to ditch EntityFieldQuery in favor of a SQL-query with db_select instead. This mostly because MySQL provides the ACID, while all the "no-sql" db's needs to break atleast one of those.

4
  • thank you! going to db_select? mmmhhhhh ... The matter is that ... if I switch to db_select instead I would probably have to face multiple/complex joins conditions to query my content fields values (quite a lot) ... and still have to do the same with taxonomies term values, too. With EntityFieldQuery I have easy access (and can easily query) at least my content (cck) field values ... isn't it? Which would be the advantage of db_select, that I don't get?
    – itamair
    Jan 17, 2012 at 10:32
  • Added my reasoning for this to the answer. :)
    – Letharion
    Jan 17, 2012 at 10:41
  • Thanks again Letharion. Your reasoning sounds nice, and quite technical. Now my concern is reduce and keep manageable the query code. Would with db_select have to write so many joins & fields conditions, as I am worrying about? or am I missing some easier approach with db_select itself?
    – itamair
    Jan 17, 2012 at 10:54
  • 1
    You'd be better off with using Views to generate the query for you. It's rather tedious manually. Also, Letharion is right, only Drupal 8 has relationship support for entity queries. Finally, if you want speed, you do need MongoDB ;)
    – user49
    Jan 20, 2013 at 2:52

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.