mrkrstphr
5/5/2015 - 6:57 PM

Sorting

Sorting

<?php

use Vnn\Core\Entity\Team;
use Vnn\Persistence\Query\Request\RequestQuery;

describe('Vn\Persistence\Query\AbstractDoctrineQuery', function () {
    describe('->sort()', function () {
        beforeEach(function () {
            $this->query = new RequestQuery($this->createEntityManager());
        });

        it('should sort the results as requested', function () {
            $results = $this->query->sort('request.created', 'ASC')->getIterator();
            expect($results)->to->have->length(6);
            expect($results[0]->getCreated()->format('Y-m-d'))->to->equal('1986-06-20');
            expect($results[count($results) - 1]->getCreated()->format('Y-m-d'))->to->equal('1991-06-20');
        });

        it('should sort the results in the order requested', function () {
            $results = $this->query->sort('request.created', 'DESC')->getIterator();
            expect($results[0]->getCreated()->format('Y-m-d'))->to->equal('1991-06-20');
            expect($results[count($results) - 1]->getCreated()->format('Y-m-d'))->to->equal('1986-06-20');
        });

        it('should allow sorting joined entities', function () {
           $results = $this->query->withTeam()->sort('team.name')->getIterator();
           expect($results[0]->getCoachTeam()->getName())->to->equal('Boys Varsity Abercrombie Modeling');
           expect($results[0]->getCoachTeam()->getName())->to->equal('Boys Varsity Abercrombie Modeling');
        });
    });
});

Sorting

Added a sort() method:

$query->sort('alias.column', 'ASC');

See real life examples in sorting.php.

I don't like that, in order to use this, you have to have knowledge of how aliasing happens within the actual DoctrineQuery object.

How to fix?