I am very confused about this behavior of EntityQuery, or maybe I am just missing something basic about PHP.
Here is the query I am working with:
$entity_query_points1 = \Drupal::entityQuery($entity_type_id)
->condition($entity_type->getKey('bundle'), $entity_type_bundle)
->condition($entity_type->getKey('uid'), $uid)
->condition('field_int_points', 1, '=');
return $entity_query_points1;
This is fine.
But if this query returns no results, I wanted to immediately execute another query. So I made a switch
statement and wrote code like this:
$entity_query_points1 = \Drupal::entityQuery($entity_type_id)
->condition($entity_type->getKey('bundle'), $entity_type_bundle)
->condition($entity_type->getKey('uid'), $uid)
->condition('field_int_points', 1, '=');
$entity_query = $entity_query_points1;
$count = $entity_query->count()->execute();
if ($count >= 1) {
return $entity_query_points1;
}
This fails with a WSOD (I am using this code with the JSON:API Resources module):
TypeError: Argument 2 passed to Drupal\jsonapi_resources\Resource\EntityQueryResourceBase::loadResourceObjectsByEntityIds() must be of the type array, string given, called in /app/web/modules/contrib/jsonapi_resources/src/Resource/EntityQueryResourceBase.php on line 97 in Drupal\jsonapi_resources\Resource\EntityQueryResourceBase->loadResourceObjectsByEntityIds() (line 125 of modules/contrib/jsonapi_resources/src/Resource/EntityQueryResourceBase.php).
If I use the following code, it works (query resolved correctly, no WSOD):
$entity_query_points1 = \Drupal::entityQuery($entity_type_id)
->condition($entity_type->getKey('bundle'), $entity_type_bundle)
->condition($entity_type->getKey('uid'), $uid)
->condition('field_int_points', 1, '=');
$entity_query = $entity_query_points1;
$count = $entity_query->count()->execute();
if ($count >= 1) {
$entity_query_score1 = \Drupal::entityQuery($entity_type_id)
->condition($entity_type->getKey('bundle'), $entity_type_bundle)
->condition($entity_type->getKey('uid'), $uid)
->condition('field_int_points', 1, '=');
return $entity_query_points1;
}
But... why? I executed the count on $entity_query
, but I have to redefine $entity_query_score1
to return the query as expected. I know having to redefine the query means I have screwed up somehow, but I don't know what my mistake is.
$entity_query = clone $entity_query_points1;
because objects might behave different I thinkclone
works; then I no longer need to redefine the query.