Permalink
Browse files

Merge pull request #549 from FabioBatSilva/DDC-1376

[DDC-1376] Support for order by association when using findBy
  • Loading branch information...
2 parents 6f572a6 + 6074755 commit b30b06852bc4eacf8bbbed472e1d6ec21310ec0a @guilhermeblanco guilhermeblanco committed Jan 14, 2013
@@ -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);
}
/**
@@ -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;
@@ -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 = 'test1@test.com';
+ $email2->email = 'test2@test.com';
+ $email3->email = 'test3@test.com';
+
+ $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();
@@ -477,6 +522,24 @@ public function testFindByOrderBy()
}
/**
+ * @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
*/
public function testFindFieldByMagicCallOrderBy()
@@ -538,7 +601,6 @@ public function testDefaultRepositoryClassName()
}
-
/**
* @group DDC-753
* @expectedException Doctrine\ORM\ORMException
@@ -551,6 +613,18 @@ public function testSetDefaultRepositoryInvalidClassError()
}
/**
+ * @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
*/
public function testInvalidOrientation()

0 comments on commit b30b068

Please sign in to comment.