0

I am working on a query filtering form that lists entities. I have various 'range' fields and I would like to display the min and max values. Is it possible to retrieve field values with EntityQuery?

For example I want to filter nodes whose NIDs are between 10 and 50 but I would also like to see which NID is the lowest and which is the highest so I can change my input accordingly.

Or better, imagine a commerce product price. I would like to see products whose price is between 10€ and 50€ but I would like to see what is the range that I can operate with(ie. from 0.99€ for the cheapest product, up to 150€ for the most expensive product).

I am thinking that with EQ this is possible only by having two queries - one for the min value and one for the max value, sort it by desired field, load a single entity and retrieve the value from the entity object. This solution is very ineffective(< performance) and since I am already using EQ I don't want to move back to basic queries just for this since that would make EQ obsolete for me.

  • Why don't you use Views? – No one May 6 '15 at 20:48
  • 2
    Because I don't want to. – user21641 May 10 '15 at 19:11
2

No, it is not possible.

There is no API to get values of specific fields. Views also doesn't do that anymore, it always loads entities as well.

  • To second Berdir's answer, the performance concerns aren't as great as you would expect. In Drupal 7 and 8, the field cache prevents most of those entity loads from even needing to query the field tables when loading an entity. Using the Entitycache module will also dramatically reduce the number of queries required for loading an entity. – Mikey P May 8 '15 at 3:14
  • In D8 you no longer have a field cache; you have Entitycache in core. – user49 May 8 '15 at 8:30
2

For example I want to filter nodes whose NIDs are between 10 and 50 but I would also like to see which NID is the lowest and which is the highest

$result = \Drupal::entityQueryAggregate('node')
  ->aggregate('nid', 'MIN', NULL, 'nid_min')
  ->aggregate('nid', 'MAX', NULL, 'nid_max')
  ->condition('nid', [10, 50], 'BETWEEN')
  ->execute();

$result[0]['nid_min'] and $result[0]['nid_max']

  • Thanks! Any idea why the field alias is reference? \Drupal\Core\Entity\Query\QueryBase::aggregate() – user21641 May 8 '15 at 16:18
  • I do not understand the question. – user49 May 8 '15 at 19:31
  • The fourth argument of the aggregate() method is a reference, so the code that you have provided will throw error because the 'nid_min' and 'nid_max' have to be references, not values. So I was wondering why is this necessary. – user21641 May 9 '15 at 9:55
  • A bit OT but how could I retrieve the condition(s) applied to the entityQuery so I could use them on the entityQueryAggregate query to find out the values I wanted(the fields)? – user21641 May 9 '15 at 13:51
  • You are adding too many comments, please write a new question. The 4th argument is not a reference, it's an alias. Check QueryBase::aggregate and QueryAggregate::addAggregate. – user49 May 9 '15 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy