Fixed 2nd parameter to Finder::order() ignored when $field is array

DragonByte Tech

Well-known member
Affected version
2.0.0 RC3
Given the following input:
PHP:
$this->finder('XF:User')->order(['custom_title', 'username'], 'DESC')->fetch();
(Yes I know the sort conditions don't make any sense, but bear with me for illustration purposes)

The results are presented in ascending order.

However, if you give the following input:
PHP:
$this->finder('XF:User')->order([['custom_title', 'DESC'], ['username', 'DESC']])->fetch();

The order will be given correctly.

The cause of this bug is the following block of code from /src/XF/Mvc/Entity/Finder.php
PHP:
            foreach ($field AS $entry)
            {
                if (is_array($entry))
                {
                    $this->order($entry[0], isset($entry[1]) ? $entry[1] : 'ASC');
                }
                else
                {
                    $this->order($entry);
                }
            }

First of all, I believe the hard-coded ASC should be changed to $direction, and the $this->order($entry); should be changed to $this->order($entry, $direction);.

For error prevention, the following block:
PHP:
            $direction = strtoupper($direction);
            if (!$direction)
            {
                $direction = 'ASC';
            }

            switch ($direction)
            {
                case 'ASC':
                case 'DESC':
                    break;

                default:
                    throw new \InvalidArgumentException("Unknown order by direction $direction");
            }

Should probably be moved to the top of the function.


Fillip
 
Back
Top Bottom