From 9705ee89d9d63d595cce59a656aa2f82399188cd Mon Sep 17 00:00:00 2001 From: nemekzg Date: Mon, 5 Nov 2012 19:55:54 +0100 Subject: [PATCH] Proposed fix for DDC-1241 --- lib/Doctrine/ORM/EntityRepository.php | 5 +++-- .../ORM/Persisters/BasicEntityPersister.php | 5 +++-- .../Tests/ORM/Functional/EntityRepositoryTest.php | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index cd4628dc0..ad68a4270 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -160,13 +160,14 @@ class EntityRepository implements ObjectRepository, Selectable * Finds a single entity by a set of criteria. * * @param array $criteria + * @param array|null $orderBy * @return object */ - public function findOneBy(array $criteria) + public function findOneBy(array $criteria, array $orderBy = null) { $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); - return $persister->load($criteria, null, null, array(), 0, 1); + return $persister->load($criteria, null, null, array(), 0, 1, $orderBy); } /** diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 22ca06ea6..ff26084a0 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -655,12 +655,13 @@ class BasicEntityPersister * @param array $hints Hints for entity creation. * @param int $lockMode * @param int $limit Limit number of results + * @param array $orderBy Criteria to order by * @return object The loaded and managed entity instance or NULL if the entity can not be found. * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? */ - public function load(array $criteria, $entity = null, $assoc = null, array $hints = array(), $lockMode = 0, $limit = null) + public function load(array $criteria, $entity = null, $assoc = null, array $hints = array(), $lockMode = 0, $limit = null, array $orderBy = null) { - $sql = $this->_getSelectEntitiesSQL($criteria, $assoc, $lockMode, $limit); + $sql = $this->_getSelectEntitiesSQL($criteria, $assoc, $lockMode, $limit, null, $orderBy); list($params, $types) = $this->expandParameters($criteria); $stmt = $this->_conn->executeQuery($sql, $params, $types); diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index 2ce949478..46f8201f3 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -344,6 +344,20 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals($addressId, $address->id); } + /** + * @group DDC-1241 + */ + public function testFindOneByOrderBy() + { + $this->loadFixture(); + + $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); + $userAsc = $repos->findOneBy(array(), array("username" => "ASC")); + $userDesc = $repos->findOneBy(array(), array("username" => "DESC")); + + $this->assertNotSame($userAsc, $userDesc); + } + /** * @group DDC-817 */