1

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

6
  • What's this notExists? What's with .target_id like shown in the linked answers?
    – leymannx
    Commented Mar 19, 2022 at 18:57
  • 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
    Commented Mar 19, 2022 at 22:36
  • 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.
    – sonfd
    Commented Mar 19, 2022 at 22:38
  • 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 | ...
    – Gaetano
    Commented Mar 20, 2022 at 17:04
  • 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
    – Gaetano
    Commented Mar 20, 2022 at 17:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.