XF 2.0 Order by phrase

CMTV

Well-known member
Hi!

I am getting my entitites from database with this code:
PHP:
return $this->finder('UserProgression:Title')->with('MasterTitle');

Later I fetch the results:
PHP:
$titles = $this->getTitleRepo()->getTitleListData()->fetch();

// >>>Sort by $title->title field?<<<

$viewParams = [
    'titles' => $titles
];

How can I sort user titles by the value ($title->title) of phrase?
 
I don't think we directly expose sorting from collections, so you would likely need to turn it into an array (toArray) and then do a custom (uasort) function on the results to be able to sort based on the value of a sub-object.
 
PHP:
namespace UserProgression\Finder;

use XF\Mvc\Entity\Finder;

/**
 * Class Title
 * @package UserProgression\Finder
 */
class Title extends Finder
{
    /**
     * @param $match
     * @param bool $caseSensitive
     * @param bool $prefixMatch
     * @return $this
     */
    public function searchText($match, $caseSensitive = false, $prefixMatch = false)
    {
        if ($match)
        {
            $expression = 'MasterTitle.phrase_text';
            if ($caseSensitive)
            {
                $expression = $this->expression('BINARY %s', $expression);
            }

            $this->where($expression, 'LIKE', $this->escapeLike($match, $prefixMatch ? '?%' : '%?%'));
        }

        return $this;
    }

    /**
     * @param string $direction
     * @return $this
     */
    public function orderTitle($direction = 'ASC')
    {
        $expression = $this->expression('CONVERT (%s USING utf8)', 'MasterTitle.phrase_text');
        $this->order($expression, $direction);

        return $this;
    }
}

PHP:
return $this->finder('UserProgression:Title')->with('MasterTitle')->orderTitle();

:)


Fillip
 
Top Bottom