XF 2.1 Finder limit min value is 1

CMTV

Well-known member
Hi!

AbstractAdapter class, line 560:
PHP:
public function limit($query, $amount, $offset = 0)
{
    $offset = max(0, intval($offset));

    if ($amount === null)
    {
        if (!$offset)
        {
            // no limit
            return $query;
        }

        // no amount limit, but there's an offset
        $amount = 1000000;
    }
    $amount = max(1, intval($amount));

    return "$query\nLIMIT $amount" . ($offset ? " OFFSET $offset" : '');
}

As you can see, the following line
PHP:
$amount = max(1, intval($amount));
sets the minimum value of limit to 1.

Why so? Sometimes it is very convenient to limit the results by some variable which value can be 0 and return an empty AbstractCollection then (when using finder)...

🤔
 
I am talking about retrieving a certain amount of entities.
PHP:
$amount = 32; // Possibly 0

return \XF::finder(...)->limit($amount)->fetch();

This example will not work for $amount = 0 since the minimum limit value is 1. And this is a problem (in my opinion).

So the correct example would be:
PHP:
$amount = 32; // Possibly 0

if ($amount === 0)
{
    return [];
}

return \XF::finder(...)->limit($amount)->fetch();
 
Last edited:
Top Bottom