I have a self-defined entity with revisions called command. The instance with id=1 has 3 revisions. Now I want to load all these revisions. Tried this:

$query = new EntityFieldQuery();

  ->entityCondition('entity_type', 'command')
  ->propertyCondition('id', 1)
$result = $query->execute();

but it gives me still only one revision, the current one. There's no difference to calling it without ->age(FIELD_LOAD_REVISION).

What am I doing wrong?

  • Are you cycling through all the results? i.e. $query->fetchAll(); – Alexander Hripak Nov 22 '12 at 20:18
  • Just thought "OMG, right!" But then adding $result = $result->fetchAll(); to the code above just gave me the error Call to a member function fetchAll() on a non-object. And it seems that EntityFieldQuery doesn't have a fetch function. – Georg Jähnig Nov 22 '12 at 20:55
  • I'm pretty sure you can just drop the execute() and do $result = $query->fetchAll() – Alexander Hripak Nov 23 '12 at 0:56
  • 1
    OK, so tried that code $query->entityCondition('entity_type', 'command')->propertyCondition('id', 1)->age(FIELD_LOAD_REVISION); $result = $query->fetchAll(); and still got Call to undefined method.... Maybe you're mixing it up with db_select()? – Georg Jähnig Nov 23 '12 at 11:46
  • 1
    I also can't get this working... – Mark Theunissen Apr 18 '13 at 19:01

Solution: Use a regular query instead of a EntityFieldQuery to load all entity revisions.


In the age function on entity.inc there's this comment:

Blockquote * Note that this only affects field conditions. Property conditions always * apply to the current revision.

So as long as you provide a property condition, you will get only the last revision id. If you query with fieldCondition, you will get the revisions, as the age method is actually querying the field revision table and not the node revision one (field_sql_storage.module):

  else {
    $tablename_function = '_field_sql_storage_revision_tablename';
    $id_key = 'revision_id';

Otherwise, the entity table is queried so it will always return the entities and not the revisions.

You could do a addTag and modify the query object there but at this point the modifications will be so big that it's actually better to do a regular query instead.

Example using generic info for the entity and entity api entity_revision_load.

  $entity_type = 'command';
  $value = 1;
  $entity_info = entity_get_info($entity_type);

  $query = db_select($entity_info['revision table'], 'revision');
  $query->fields('revision', array($entity_info['entity keys']['id'], $entity_info['entity keys']['revision']))
    ->condition('revision.' . $entity_info['entity keys']['id'], $value);
  $revisions = $query
    ->fetchAllAssoc($entity_info['entity keys']['revision']);

  $entities = array();
  foreach ($revisions as $key => $revision) {
    $entities[] = entity_revision_load($entity_type, $key);

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.