RSS

Newsletter

The CodeKarate Newsletter is the best way for technical ninjas to keep their swords sharpened. Don't worry, we won't flood your inbox and your email address will always remain private.
Back to Top

Drupal 7 EntityFieldQuery()

So I finally got around to looking into module development with Drupal 7. In fact, I really started to get my hands on Drupal 7 for the first time. I was very pleasantly surprised with the admin overlay and the entire usability of Drupal 7.

While building a module with Features, Views, and Fields (most of CCK was rolled up into Drupal 7 core with some changes and is now called Fields), I quickly ran into a problem. I wanted to do a simple query of node data. In Drupal 6 I would have accomplished this with a simple select statement. Something like:

//Note: this is an untested example but should be pretty close
$sql = "SELECT ct.field_myfield_value FROM {node} n
  INNER JOIN {content_type_mytype} ct ON n.nid = ct.nid
  WHERE status = %d AND field_myfield2_value = %d";
$results = db_query($sql, 1, 12);

While in Drupal 7 I did this to accomplish the same thing.

$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'mytype')
->propertyCondition('status', 1)
->fieldCondition('field_myfield2', 'value', '12', '=')
->execute();
 
$nodes = entity_load('node', array_keys($entities['node']));

This returns a collection of loaded nodes with all necessary node values. On first glance this seems to be much less efficient and a very roundabout way of getting to the single value I needed. However, Drupal 7 Entities really changed the database structure of how field data is stored, making something like this necessary.

Perhaps someone out there can shed some additional light, or point me to a discussion on how this is better. I like the fact that there is an API type interface for making these queries, but I never minded writing SQL and I am a little concerned on the scalability of this with very large websites.

I don't doubt that it has numerous benefits, but I just recently made the transition from Drupal 6 to Drupal 7 (not all the way yet, but I am getting there), and am anxious to learn more.

Discussions

1
Drew (not verified)

Caching

I think caching is a benefit of the new DB/querying system.
I'n not really sure how, but I think it allows the queries to be cached better than D6, and the new system means the DB can be swapped out for different DB systems that can improve performance when you are dealing with large sites.

1-1
shane

Thanks for the comment.

Thanks for the comment. The caching benefit definitely sounds possible, and the ability to easily swap DB back-ends without any issues is another huge plus.

I am starting to really appreciate the object oriented nature of the new DB system rather than having to manually write all the simple SQL queries that I normally have to write.

shane's picture
2
keshav (not verified)

between not woring

Hi

$query->join('field_data_field_amountpaid', 'amt', 'n.nid = amt.entity_id and amt.field_amountpaid_value >=500 and amt.field_amountpaid_value <=5000 '); //JOIN node with Body

this is my normal query how can i call the in the enttiy query

i try this way

$query->fieldCondition('field_amountpaid', 'value', array('501','5000'),'BETWEEN');

but it's not working. any idea.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <blockquote> <img>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <php>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <python>, <ruby>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.