Score:0

EntityQuery, exclude nodes with NOT IN from a multivalued entity reference field

cn flag

Drupal v. 9.3.8 I recently used the entityQuery command and for simpler queries I had no problems. Now I need to filter, as well as for the '=' and 'IN' operators, also the nodes, whose multivalued field has no values ​​or which does not have certain values. Reading the documentation I used the 'NOT IN' operator, but with poor results. Example the query should return me the nodes that do not have the value 161 and 163 This is the query I simplified to try the 'NOT IN' operator

$nids = \Drupal::entityTypeManager()->getStorage('node')->getQuery()
->condition('type'   , 'gestione' )
->notExists('field_attivita')
->condition( 'field_attivita', ['161', '163'], 'NOT IN' )
->execute();

I also tried with the '<>' operator but nothing to do

Among the various articles and questions I also read the post that comes close to my request How to use entityQuery to select if a multiple value entityReference field does not contain the id but I can't understand why it doesn't work

leymannx avatar
ne flag
What's this notExists? What's with .target_id like shown in the linked answers?
sonfd avatar
in flag
I'm not sure, but I don't think you can do this in a single query. I think you may need to 1. query for all, 2. query for the items with the ids you don't want, and then remove the ids from query 2 from the result set of query 1. Which, btw, is basically what the linked answer is saying.
sonfd avatar
in flag
I think the issue here is how the entity reference values are stored - each value in a multi-value field gets its own row in the db table. If your field had cardinality = 1, you could use not equals for each value you wanted to exclude.
cn flag
sonfd, thanks for the reply and for the explanation. As mentioned, I recently approached queries (sorry also my elementary English, it will also be for this reason that I will be unclear) I did some tests based on your suggestions and maybe I understood what you said to me. But there is one thing I don't understand. I have written some data for an example Pos. Field_activity | $ var 1 300 | 161 2 161 | 163 3 400 | 4 163 | 5 empty | 6 200 | 7 163 | 8 empty | ...
cn flag
if I use the condition -> condition ('conflict_activity_field', [161, 163], 'IN') it brings me back the nodes in position 2 - 4 and 7 of the example -> condition ('conflict_activity_field', [161, 163], 'NOT IN') should only destroy nodes that do not contain 161 and 163 Otherwise I didn't understand what the 'NOT IN' operator does the SQL manual confirms it I am puzzled to do 2 queries and then the difference between the arrays, the query is complex and a lot of data may slow down the processing
cn flag
leymannx, thanks for the reply but i didn't get it. notExist indicates whether the multivalued field has no data. target_id is the node's nid
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.