According to the documentation for EntityFieldQuery::range if you pass NULL as argument, the range directive gets removed.
This is not working as expected. It is not removing the range directive, it is just setting the $start and $length values as NULL.

To reproduce, construct a query as normal, including a range(0,10) condition. Then, set range(NULL, NULL) - or just range() - and execute again.

In entity.inc line 1314, it checks if ($this->range) - which in this case would result in:

range = array(
  'start' => NULL,
  'length' => NULL,
)

being evaluated as true.

Should the range() function not check if ($start === NULL) before assigning the arguments to the object property? Alternatively, finishQuery() needs to check the individual elements instead of the array.

Comments

andyg5000’s picture

Version: 7.19 » 7.x-dev
Component: entity system » database system
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1 KB

The ->range() method in entity.inc and includes/database/select.inc both state "The first entity from the result set to return. If NULL, removes any range directives that are set." which is not true as stated by @MaggotMouth above.

This function should only set range values on the query object if the $start argument is something other than NULL. Not doing so generates the following in MYSQL.

LIMIT 0 OFFSET 0

We need to either update these methods to match the documentation of them, or update the documentation. I propose the former so that you can call ->range() ->range(NULL) or ->range(NULL, NULL) and get the correct results.

Patch attached.

Status: Needs review » Needs work

The last submitted patch, 1: drupal-database_range_query_null_issue-1943754-1.patch, failed testing.

andyg5000’s picture

StatusFileSize
new1 KB

Whops should be !== since $start will frequently be 0.

andyg5000’s picture

Status: Needs work » Needs review

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.