0

I have a syntax issue on using conditional clauses with EFQ, but the documentation on drupal.org is a bit unclear for me. I have google the whole Internet for this with no results at all. My hook implementation is based on another drupal.stackexchange question

The code works for a single ->fieldCondition but when I try to implement the hook below,

    $query = new EntityFieldQuery();
    $entityFormSubmissionIds = $query->entityCondition('entity_type', 'entityform')
      ->addTag('or')
      ->execute();

/** Implements hook_query_TAG_alter

@param QueryAlterableInterface $query */

function mymodule_query_or_alter(QueryAlterableInterface $query) {
 $or = db_or()->condition('field_a',$term->tid)
       ->condition('field_B', $term->tid);
    ->condition($or)
    ->execute();

Fatal error: Call to undefined method DatabaseCondition::execute()

  • When you alter the query it's not a EFQ any more, it's just a standard query. So you can't use fieldCondition and other EFQ-specific methods. You'll need to use the usual condition method – Clive Nov 26 '18 at 8:56
  • If you are setting your conditions like that, it won't work. You need to add the conditions to the db_or object then add the db_or to the query. – Alfred Armstrong Nov 26 '18 at 9:37
  • @Clive I edited my question based on your comment. – staminna Nov 26 '18 at 9:37
  • @AlfredArmstrong, Can you provide a small example? – staminna Nov 26 '18 at 9:38
  • Instead of $query->condition('field_A1', $my->tid) write $db_or->condition(...) and so on for your other fields. That creates an OR clause containing those fields, which you then add to your query using the condition method on the query itself. – Alfred Armstrong Nov 26 '18 at 9:42
0

In the alter hook, you should not be calling execute(). You don't want to run the query at this point, just change it.

function mymodule_query_or_alter(QueryAlterableInterface $query) {
 $or = db_or()->condition('field_a',$term->tid)
       ->condition('field_B', $term->tid);
 $query->condition($or)
}
  • PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_A' in 'where clause': SELECT entityform.entityform_id AS entity_id, entityform.type AS bundle, :entity_type AS entity_type, NULL AS revision_id FROM {entityform} entityform WHERE ( (field_A = :db_condition_placeholder_0) OR (field_A_s = :db_condition_placeholder_1) )AND( (field_B[:entity_type] => entityform ) in EntityFieldQuery->execute() (line 1216 of entity.inc). – staminna Nov 26 '18 at 10:41
  • field_a and fields_b are part of fieldable entity of type entityform, but it looks like EFQ is looking to compare entityform ids's with they taxonomy term field_a and b term id's. – staminna Nov 26 '18 at 10:45
  • This is because those fields are not part of the base table. Thinking about it you will be better off forgetting about EFQ and starting with a db_select instead. Then you won't need an alter and you can handcraft the query exactly to you needs. – Alfred Armstrong Nov 26 '18 at 10:48
  • The thing is I don't know how to db_select entityform. The fields are not in it's table, and everything else is inside a Blob – staminna Nov 26 '18 at 12:03
  • Fields are never in an entity's table. They will be in tables called "field_data_NAME_OF_FIELD'. So you have to join them as part of the query. There's an example here drupal.org/docs/7/api/database-api/dynamic-queries/joins – Alfred Armstrong Nov 26 '18 at 12:29

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.