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()

6
  • 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
    Commented Nov 26, 2018 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. Commented Nov 26, 2018 at 9:37
  • @Clive I edited my question based on your comment.
    – staminna
    Commented Nov 26, 2018 at 9:37
  • @AlfredArmstrong, Can you provide a small example?
    – staminna
    Commented Nov 26, 2018 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. Commented Nov 26, 2018 at 9:42

1 Answer 1

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)
}
5
  • 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
    Commented Nov 26, 2018 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
    Commented Nov 26, 2018 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. Commented Nov 26, 2018 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
    Commented Nov 26, 2018 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 Commented Nov 26, 2018 at 12:29

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.