1

Extending EntityFieldQuery's method finishQuery with the following code causes query return nothing:

class ClientEntityFieldQuery extends EntityFieldQuery {

  public function finishQuery($select_query, $id_key = 'entity_id') {
    $select_query->leftJoin('field_data_field_date', 'o', 'client_event.eid = o.entity_id');
    $or = db_or()
      ->condition('o.field_date_value', time(), '<')
      ->isNull('o.field_date_value');
    $select_query->condition($or);

    // Call parent method
    parent::finishQuery($select_query, $id_key);
  }
}

But when I copy the contents of finishQuery to the extended class (see below), the query works fine. So why does it not work when altering $select_query in the extended class and then calling parent::finishQuery($select_query, $id_key)?

This works:

class ClientEntityFieldQuery extends EntityFieldQuery {

  public function finishQuery($select_query, $id_key = 'entity_id') {
    $select_query->leftJoin('field_data_field_date', 'o', 'client_event.eid = o.entity_id');
      $or = db_or()
        ->condition('o.field_date_value', time(), '<')
        ->isNull('o.field_date_value');
    $select_query->condition($or);

    // This is copypaste from EntityFieldQuery's method finishQuery
    foreach ($this->tags as $tag) {
      $select_query->addTag($tag);
    }
    foreach ($this->metaData as $key => $object) {
      $select_query->addMetaData($key, $object);
    }
    $select_query->addMetaData('entity_field_query', $this);
    if ($this->range) {
      $select_query->range($this->range['start'], $this->range['length']);
    }
    if ($this->count) {
      return $select_query->countQuery()->execute()->fetchField();
    }
    $return = array();
    foreach ($select_query->execute() as $partial_entity) {
      $bundle = isset($partial_entity->bundle) ? $partial_entity->bundle : NULL;
      $entity = entity_create_stub_entity($partial_entity->entity_type, array($partial_entity->entity_id, $partial_entity->revision_id, $bundle));
      $return[$partial_entity->entity_type][$partial_entity->$id_key] = $entity;
      $this->ordered_results[] = $partial_entity;
    }
    return $return;
  }

}

1 Answer 1

1

I think it's because you're not returning anything from the function.

The code for the base finishQuery() function returns at the very least an empty array; at the moment your function isn't doing that.

The fix might be as simple as changing the last line to:

return parent::finishQuery($select_query, $id_key);
2
  • This was it, thanks heaps. So in case parent method returns something, child class also have to return, i.e. return parent::method() instead of just parent::method().
    – Lauri
    Commented May 24, 2012 at 0:02
  • It's always good practice to do that, yeah. The return value might not be used in some cases but you won't know unless you digging around to find out, which can be a pain. It's easier to just provide the return value any time you override a function like this.
    – Clive
    Commented May 24, 2012 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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