Skip to content

Commit

Permalink
Merge pull request #567 from greg0ire/postHawk-565-fix
Browse files Browse the repository at this point in the history
Post hawk 565 fix
  • Loading branch information
greg0ire committed May 26, 2016
2 parents 4990b74 + 4da0785 commit 7adaffd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
20 changes: 16 additions & 4 deletions Datagrid/ProxyQuery.php
Expand Up @@ -11,6 +11,7 @@

namespace Sonata\DoctrineORMAdminBundle\Datagrid;

use Doctrine\Common\Collections\Criteria;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
Expand Down Expand Up @@ -113,11 +114,22 @@ public function execute(array $params = array(), $hydrationMode = null)
->getMetadataFor(current($queryBuilder->getRootEntities()))
->getIdentifierFieldNames();

$existingOrders = array();
/** @var Query\Expr\OrderBy $order */
foreach ($queryBuilder->getDQLPart('orderBy') as $order) {
foreach ($order->getParts() as $part) {
$existingOrders[] = trim(str_replace(array(Criteria::DESC, Criteria::ASC), '', $part));
}
}

foreach ($identifierFields as $identifierField) {
$queryBuilder->addOrderBy(
$rootAlias.'.'.$identifierField,
$this->getSortOrder() // reusing the sort order is the most natural way to go
);
$order = $rootAlias.'.'.$identifierField;
if (!in_array($order, $existingOrders)) {
$queryBuilder->addOrderBy(
$order,
$this->getSortOrder() // reusing the sort order is the most natural way to go
);
}
}

return $this->getFixedQueryBuilder($queryBuilder)->getQuery()->execute($params, $hydrationMode);
Expand Down
16 changes: 13 additions & 3 deletions Tests/Datagrid/ProxyQueryTest.php
Expand Up @@ -13,6 +13,7 @@

use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Query\Expr\From;
use Doctrine\ORM\Query\Expr\OrderBy;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\UuidType;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Util\NonIntegerIdentifierTestClass;

Expand Down Expand Up @@ -99,10 +100,19 @@ public function testGetFixedQueryBuilder($class, $alias, $id, $expectedId, $valu
$qb->expects($this->once())
->method('setParameter')
->with($this->equalTo($expectedId), $this->equalTo(array($value)));
$qb->expects($this->once())
$qb->expects($this->any())
->method('getDQLPart')
->with($this->equalTo('from'))
->willReturn(array(new From($class, $alias)));
->will($this->returnCallBack(function ($part) use ($class, $alias) {
$parts = array(
'from' => array(new From($class, $alias)),
'orderBy' => array(new OrderBy('whatever', 'DESC')),
);

return $parts[$part];
}));
$qb->expects($this->once())
->method('addOrderBy')
->with("$alias.$id", null);
$qb->expects($this->once())
->method('getRootEntities')
->willReturn(array($class));
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -32,6 +32,7 @@
"require-dev": {
"knplabs/knp-menu-bundle": "^2.1.1",
"simplethings/entity-audit-bundle": "~0.1",
"sllh/php-cs-fixer-styleci-bridge": "^2.0",
"symfony/phpunit-bridge": "^2.7 || ^3.0"
},
"suggest": {
Expand Down

0 comments on commit 7adaffd

Please sign in to comment.