19

Drupal 7 has very promising ORM-like system of queries with EntityFieldQuery.

Currently, I understand how to make selection of nodes, but the result doesn't contain any specific information, like fileds, I'm looking for.

$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
                        ->entityCondition('bundle', 'event')
                        ->propertyCondition('status', 1)
                        ->fieldCondition('field_date', 'value', array('2011-03-01', '2011-03-31'), 'BETWEEN')
                        ->fieldOrderBy('field_date', 'value', 'ASC')
                        ->execute();

So we need to load entire data of nodes, if we would like to get only values of one field, for example 'field_date' in current example.

$nodes = entity_load('node', array_keys($entities['node']));

If there is a way to get the value of fields, instead to load full data of all nodes, as it provokes some memory overload.

$nodesFieldDates = ???
10

Great question!

EntityFieldQuery is truly good stuff, but if you really want to get serious, you have to override the class in a custom module and add whatever behavior you need done in there.

I don't know your exact intentions, but I have referred myself to this article on the subject in the past. Credits to Neil Hastings for this well written write-up.

Look for code examples a bit into the article for nice examples of overiding.

Hope that helps, happy coding!

  • Ok, I see ! So, probably, it's possible to redefine "EntityFieldQuery" "buildQuery" method, to alternate resulting fields. Probably, this could work. – Fedir RYKHTIK Dec 22 '11 at 10:47
11

I found the answer for this! The Apache Solr module extends the EntityFieldQuery class in order to make it happen. It adds a new method called addExtraField.

<?php
$query = new ApachesolrAttachmentsEntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'event')
  ->propertyCondition('status', 1)
  ->fieldCondition('field_date', 'value', array('2011-03-01', '2011-03-31'), 'BETWEEN')
  ->fieldOrderBy('field_date', 'value', 'ASC')
  ->addExtraField('field_date', 'value', 'value')
  ->execute();
?>

If you don't have Apache Solr installed simply copy the code for extending that class.

8

You don't need to override anything or extend the class. You just need to use field_attach_load() to attach the field you want to the results from the EntityFieldQuery class.

See also Loading Only One Field From An Entity or Node in Drupal 7, a good article with examples on how to do it.

0

Anyone still wanting to get Fields from a EFQ take a look at the EFQ Extra Fields module.

Warning : This module abuses EntityFieldQuery to return data from your database. It does not work with entities or any modules that build on them.

It allows you to use addField within an EntityFieldQueryExtraFields object (extends EntityFieldQuery objects). Initially EntityFieldQuery only gives you back the entity id, bundle and entity type. This module adds an extra layer on top of that without doing some extra entity_loads! Important if you want to work with massive amounts of data and you want to reduce the sql queries. Since EFQ already queries the database for this information, why not alter that query to also give you some additional fields. This means that not a single extra query will be performed to get that data.

A word of caution, if you rely on some post-formatting/post-loading that happens in entity_load or similar functions, this module won't take care of that. You have to ensure the validity of the retrieved data yourselves.

Example

Fetching the node title

$query = new EntityFieldQueryExtraFields();
  $result = $query->entityCondition('entity_type', 'node')
  ->propertyCondition('type', 'my_bundle_type')
  ->propertyCondition('status', 1)
  ->addExtraField('field_myfield', value', value')
  ->addExtraField('field_mynodereffield', nid', nid')
  ->addExtraField('', 'title', 'title', 'node')
  ->fieldCondition('field_myfield', 'value', 'some_value_to_filter_on', '=')
  ->execute();
Possible combinations :
addExtraField($field_name, $column, $column_alias = NULL, $table = NULL)

field_name
Specify from which field the extra field should come. If it is from a base table, leave it empty and fill in the table argument

column
Specify the name of the column,

column_alias
Specify the alias of the column,

table
Optionally, add something like node or user here (the base table),
  • I've tried both this module (EFQ Extra Fields) and the (currently sandbox) module by mikeytown2 and the latter seemed much better (nicer to use, more sensible output) and also appears to be maintained a lot better than the former. mikeytown2 sandbox module is at drupal.org/sandbox/mikeytown2/2209909. – Oliver Coleman Jan 2 '15 at 0:11

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.