Score:2

EntityQuery where multi-value field contains a value

de flag

I've been programming for 15 years, 10 with PHP, but on my 1st Drupal project. Using Drupal 9.

I'm trying to retrieve an entity (custom content type) that has 2 multi-value entity (user) fields. If the value I have is in one field I want to get the other one. The problem is I can't figure out how to ask for the entity when the scalar value I have is in the list. Here's what I've got:

return \Drupal::entityQuery('node')
  ->condition('type', 'osj_relationships')
  ->condition('field_supervised_reps', [$userId], 'IN')
  ->execute();

Which generates this SQL:

'SELECT "base_table"."vid" AS "vid", "base_table"."nid" AS "nid"
FROM
"node" "base_table"
INNER JOIN "node_field_data" "node_field_data" ON "node_field_data"."nid" = "base_table"."nid"
INNER JOIN "node__field_supervised_reps" "node__field_supervised_reps" ON "node__field_supervised_reps"."entity_id" = "base_table"."nid"
WHERE ("node_field_data"."type" = 'osj_relationships') AND ("node__field_supervised_reps"."field_supervised_reps_target_id" IN ('2458'))'

The 2nd part of the WHERE clause is looking for the field value in my parameter. I need it to look for my parameter in the field. I've also tried CONTAINS but that seems to assume the field value is a string rather than an array. If I could turn that WHERE clause around it would be perfect. ie.

(('2458') IN "node__field_supervised_reps"."field_supervised_reps_target_id") 

I suppose I could use 'CONTAINS' if I could implode the field list but that would risk false hits.

Any suggestions?

Score:2
in flag

You don't even have to use IN here, only if you have multiple $userIds, otherwise a simple = operator will work:

return \Drupal::entityQuery('node')
  ->condition('type', 'osj_relationships')
  ->condition('field_supervised_reps', $userId)
  ->execute();

The generated query will actually work how you'd like. It works because each value of a multi-value field is stored in its own row in the database.

For example, given a multi-value entity reference field, field_tags, added to article nodes, if you have an article (with id = 111), referencing two taxonomy terms with ids 222 and 333, the node__field_tags table might look like:

bundle deleted entity_id revision_id langcode delta field_tags_target_id
article 0 111 777 en 0 222
article 0 111 777 en 1 333
Ed Coakley avatar
de flag
Thanks for the reply. I only started messing with 'in' & 'contains' after '=' didn't work. In addition to the entityQuery(), I've also tried entityTypeManager(): return \Drupal::entityTypeManager() ->getStorage('node') ->loadByProperties([ 'type'=>'osj_relationships', 'field_supervised_reps'=>$userId ]); ...with the same result (empty array). I've verified the target I'm looking for is in the database and the field names are right. I've tried intval($userid) which also didn't change anything. Any other suggestions?
Ed Coakley avatar
de flag
Well, upon closer inspection, I discovered a spelling problem when I created the content type. As always, the stupid machine is doing what it's told. 'osj_relationships' != 'osj_releationships' Ugh. Thanks for at least validating that I'm barking up the right tree.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.