1

I'd written a query based on the "simplified" version here. And my code looked like this.

$results = \Drupal::entityQuery('node')
  ->condition('type', 'mytype')
  ->condition('status', 1)
  ->condition('field_first.target_id', $anArrayOfNids)
  ->condition('field_seciond.target_id', array($anotherNid))
  ->execute();

The code was working, I know it was. But now I'm getting an "SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens" SQL error.

I tried replacing it with just array(540) and got the proper result. Then I gave it array(546) and got the different, expected response, but as soon as I put [540,560] or array(540,560), I get that error, instead of what i used to get, which was the combined results.

8
  • What's changed since it used to work? (broad overview will do...core upgrades? config changes? etc)
    – Clive
    May 2 '17 at 18:42
  • No module upgrades. Only one or two other parts of the module, and minor, that should have affected it at all, and I did everything to remove to test it; once I tried the hardcoded array and it didn't work, I was stumped. May 2 '17 at 18:46
  • Yeah, can't think of a reason that wouldn't work (but I do seem to remember seeing something similar reported somewhere recently, I'll try to dig it up)
    – Clive
    May 2 '17 at 18:47
  • 1
    Out of interest what happens if you explicitly set the operator, e.g. ->condition('field_first.target_id', [123, 456], 'IN');?
    – Clive
    May 2 '17 at 18:51
  • That fixed it. Wow. I DID not have that before, and the example off of drupal.org , doesn't have it. I'll leave a comment. I'm wondering what happened. I'm 99% sure I didn't run any OS updates on my VM until AFTER I started having that issue--because that's why I noted that there was a mysql update--but, that's the only thing I'm aware of that even comes close to explaining it. May 2 '17 at 19:10
2

I don't know how it was working before, but the solution seems to be to place the explicit 'IN' in the query as such:

$results = \Drupal::entityQuery('node')
  ->condition('type', 'mytype')
  ->condition('status', 1)
  ->condition('field_first.target_id', $anArrayOfNids, 'IN')
  ->condition('field_seciond.target_id', array($anotherNid))
  ->execute();

Or, if I was to just convert this to the more complicated, yet more robust method:

$results = \Drupal::entityQuery('node');
$group = $query->orConditionGroup();
foreach($anArrayOfNids as $nid) {
  $group->condition(field_first.target_id, array($nid));
}
$query->condition('type', 'mytype')
  ->condition('status', 1)
  ->condition('field_seciond.target_id', array($anotherNid))
  ->condition($group)
  ->execute();
1
  • The second option isn't more robust, justs more complex. Explicitly passing IN is required since before 8.0.0 but I think there's a bug that it works with an array that just has a single entry without IN
    – Berdir
    May 2 '17 at 19:49

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.