Skip to content

Commit

Permalink
Merge pull request #549 from FabioBatSilva/DDC-1376
Browse files Browse the repository at this point in the history
[DDC-1376] Support for order by association when using findBy
  • Loading branch information
guilhermeblanco committed Jan 14, 2013
2 parents 6f572a6 + 6074755 commit b30b068
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 12 deletions.
42 changes: 31 additions & 11 deletions lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -1138,29 +1138,49 @@ protected function getSelectSQL($criteria, $assoc = null, $lockMode = 0, $limit
*/
protected final function getOrderBySQL(array $orderBy, $baseTableAlias)
{
$orderBySql = '';
$orderByList = array();

foreach ($orderBy as $fieldName => $orientation) {
if ( ! isset($this->class->fieldMappings[$fieldName])) {
throw ORMException::unrecognizedField($fieldName);
}

$orientation = strtoupper(trim($orientation));

if ($orientation != 'ASC' && $orientation != 'DESC') {
throw ORMException::invalidOrientation($this->class->name, $fieldName);
}

$tableAlias = isset($this->class->fieldMappings[$fieldName]['inherited'])
? $this->getSQLTableAlias($this->class->fieldMappings[$fieldName]['inherited'])
: $baseTableAlias;
if (isset($this->class->fieldMappings[$fieldName])) {
$tableAlias = isset($this->class->fieldMappings[$fieldName]['inherited'])
? $this->getSQLTableAlias($this->class->fieldMappings[$fieldName]['inherited'])
: $baseTableAlias;

$columnName = $this->quoteStrategy->getColumnName($fieldName, $this->class, $this->platform);
$orderByList[] = $tableAlias . '.' . $columnName . ' ' . $orientation;

continue;
}

if (isset($this->class->associationMappings[$fieldName])) {

$columnName = $this->quoteStrategy->getColumnName($fieldName, $this->class, $this->platform);
if ( ! $this->class->associationMappings[$fieldName]['isOwningSide']) {
throw ORMException::invalidFindByInverseAssociation($this->class->name, $fieldName);
}

$tableAlias = isset($this->class->associationMappings[$fieldName]['inherited'])
? $this->getSQLTableAlias($this->class->associationMappings[$fieldName]['inherited'])
: $baseTableAlias;

foreach ($this->class->associationMappings[$fieldName]['joinColumns'] as $joinColumn) {
$columnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform);
$orderByList[] = $tableAlias . '.' . $columnName . ' ' . $orientation;
}

continue;
}

$orderBySql .= $orderBySql ? ', ' : ' ORDER BY ';
$orderBySql .= $tableAlias . '.' . $columnName . ' ' . $orientation;
throw ORMException::unrecognizedField($fieldName);
}

return $orderBySql;
return ' ORDER BY ' . implode(', ', $orderByList);
}

/**
Expand Down
76 changes: 75 additions & 1 deletion tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\Models\CMS\CmsEmail;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
use Doctrine\Common\Collections\Criteria;
Expand Down Expand Up @@ -89,6 +90,50 @@ public function loadAssociatedFixture()
return array($user->id, $address->id);
}

public function loadFixtureUserEmail()
{
$user1 = new CmsUser();
$user2 = new CmsUser();
$user3 = new CmsUser();

$email1 = new CmsEmail();
$email2 = new CmsEmail();
$email3 = new CmsEmail();

$user1->name = 'Test 1';
$user1->username = 'test1';
$user1->status = 'active';

$user2->name = 'Test 2';
$user2->username = 'test2';
$user2->status = 'active';

$user3->name = 'Test 3';
$user3->username = 'test3';
$user3->status = 'active';

$email1->email = '[email protected]';
$email2->email = '[email protected]';
$email3->email = '[email protected]';

$user1->setEmail($email1);
$user2->setEmail($email2);
$user3->setEmail($email3);

$this->_em->persist($user1);
$this->_em->persist($user2);
$this->_em->persist($user3);

$this->_em->persist($email1);
$this->_em->persist($email2);
$this->_em->persist($email3);

$this->_em->flush();
$this->_em->clear();

return array($user1, $user2, $user3);
}

public function buildUser($name, $username, $status, $address)
{
$user = new CmsUser();
Expand Down Expand Up @@ -476,6 +521,24 @@ public function testFindByOrderBy()
$this->assertSame($usersAsc[3], $usersDesc[0]);
}

/**
* @group DDC-1376
*/
public function testFindByOrderByAssociation()
{
$this->loadFixtureUserEmail();

$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
$resultAsc = $repository->findBy(array(), array('email' => 'ASC'));
$resultDesc = $repository->findBy(array(), array('email' => 'DESC'));

$this->assertCount(3, $resultAsc);
$this->assertCount(3, $resultDesc);

$this->assertEquals($resultAsc[0]->getEmail()->getId(), $resultDesc[2]->getEmail()->getId());
$this->assertEquals($resultAsc[2]->getEmail()->getId(), $resultDesc[0]->getEmail()->getId());
}

/**
* @group DDC-1426
*/
Expand Down Expand Up @@ -538,7 +601,6 @@ public function testDefaultRepositoryClassName()

}


/**
* @group DDC-753
* @expectedException Doctrine\ORM\ORMException
Expand All @@ -550,6 +612,18 @@ public function testSetDefaultRepositoryInvalidClassError()
$this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753InvalidRepository");
}

/**
* @group DDC-1376
*
* @expectedException Doctrine\ORM\ORMException
* @expectedExceptionMessage You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address', because it is the inverse side of an association.
*/
public function testInvalidOrderByAsssociation()
{
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
->findBy(array('status' => 'test'), array('address' => 'ASC'));
}

/**
* @group DDC-1500
*/
Expand Down

0 comments on commit b30b068

Please sign in to comment.