diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index 7bc54c847..de2689db2 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -160,11 +160,14 @@ class EntityRepository implements ObjectRepository * Finds entities by a set of criteria. * * @param array $criteria - * @return array + * @param array|null $orderBy + * @param int|null $limit + * @param int|null $offset + * @return array The objects. */ - public function findBy(array $criteria) + public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) { - return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->loadAll($criteria); + return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->loadAll($criteria, $orderBy, $limit, $offset); } /** diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 39bb967e6..454078dfe 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -680,12 +680,15 @@ class BasicEntityPersister * Loads a list of entities by a list of field criteria. * * @param array $criteria + * @param array $orderBy + * @param int $limit + * @param int $offset * @return array */ - public function loadAll(array $criteria = array()) + public function loadAll(array $criteria = array(), array $orderBy = null, $limit = null, $offset = null) { $entities = array(); - $sql = $this->_getSelectEntitiesSQL($criteria); + $sql = $this->_getSelectEntitiesSQL($criteria, null, 0, $limit, $offset, $orderBy); list($params, $types) = $this->expandParameters($criteria); $stmt = $this->_conn->executeQuery($sql, $params, $types); @@ -831,19 +834,21 @@ class BasicEntityPersister * @param AssociationMapping $assoc * @param string $orderBy * @param int $lockMode + * @param int $limit + * @param int $offset + * @param array $orderBy * @return string * @todo Refactor: _getSelectSQL(...) */ - protected function _getSelectEntitiesSQL(array $criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null) + protected function _getSelectEntitiesSQL(array $criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null) { $joinSql = $assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY ? $this->_getSelectManyToManyJoinSQL($assoc) : ''; $conditionSql = $this->_getSelectConditionSQL($criteria, $assoc); - $orderBySql = $assoc !== null && isset($assoc['orderBy']) ? - $this->_getCollectionOrderBySQL($assoc['orderBy'], $this->_getSQLTableAlias($this->_class->name)) - : ''; + $orderBy = ($assoc !== null && isset($assoc['orderBy'])) ? $assoc['orderBy'] : $orderBy; + $orderBySql = $orderBy ? $this->_getOrderBySQL($orderBy, $this->_getSQLTableAlias($this->_class->name)) : ''; $lockSql = ''; if ($lockMode == LockMode::PESSIMISTIC_READ) { @@ -869,12 +874,12 @@ class BasicEntityPersister * @return string * @todo Rename: _getOrderBySQL */ - protected final function _getCollectionOrderBySQL(array $orderBy, $baseTableAlias) + protected final function _getOrderBySQL(array $orderBy, $baseTableAlias) { $orderBySql = ''; foreach ($orderBy as $fieldName => $orientation) { if ( ! isset($this->_class->fieldMappings[$fieldName])) { - ORMException::unrecognizedField($fieldName); + throw ORMException::unrecognizedField($fieldName); } $tableAlias = isset($this->_class->fieldMappings[$fieldName]['inherited']) ? diff --git a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php index 2490085cf..1cb73a9d6 100644 --- a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php @@ -238,7 +238,7 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister /** * {@inheritdoc} */ - protected function _getSelectEntitiesSQL(array $criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null) + protected function _getSelectEntitiesSQL(array $criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null) { $idColumns = $this->_class->getIdentifierColumnNames(); $baseTableAlias = $this->_getSQLTableAlias($this->_class->name); @@ -343,10 +343,8 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister $conditionSql = $this->_getSelectConditionSQL($criteria, $assoc); - $orderBySql = ''; - if ($assoc != null && isset($assoc['orderBy'])) { - $orderBySql = $this->_getCollectionOrderBySQL($assoc['orderBy'], $baseTableAlias); - } + $orderBy = ($assoc !== null && isset($assoc['orderBy'])) ? $assoc['orderBy'] : $orderBy; + $orderBySql = $orderBy ? $this->_getOrderBySQL($orderBy, $baseTableAlias) : ''; if ($this->_selectColumnListSql === null) { $this->_selectColumnListSql = $columnList; diff --git a/lib/vendor/doctrine-common b/lib/vendor/doctrine-common index ba63ae0f0..076a03f8f 160000 --- a/lib/vendor/doctrine-common +++ b/lib/vendor/doctrine-common @@ -1 +1 @@ -Subproject commit ba63ae0f0b6b62a2a8617f01386698730ff2b713 +Subproject commit 076a03f8f40b6e08f0ae2f4ee2678474e64b6f59 diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index 0e38fe209..6c620db38 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -320,5 +320,40 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals(1, count($params), "Should only execute with one parameter."); $this->assertEquals(array('romanb'), $params); } + + /** + * @group DDC-1094 + */ + public function testFindByLimitOffset() + { + $this->loadFixture(); + + $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); + + $users1 = $repos->findBy(array(), null, 1, 0); + $users2 = $repos->findBy(array(), null, 1, 1); + + $this->assertEquals(2, count($repos->findBy(array()))); + $this->assertEquals(1, count($users1)); + $this->assertEquals(1, count($users2)); + $this->assertNotSame($users1[0], $users2[0]); + } + + /** + * @group DDC-1094 + */ + public function testFindByOrderBy() + { + $this->loadFixture(); + + $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); + $usersAsc = $repos->findBy(array(), array("username" => "ASC")); + $usersDesc = $repos->findBy(array(), array("username" => "DESC")); + + $this->assertEquals(2, count($usersAsc), "Pre-condition: only two users in fixture"); + $this->assertEquals(2, count($usersDesc), "Pre-condition: only two users in fixture"); + $this->assertSame($usersAsc[0], $usersDesc[1]); + $this->assertSame($usersAsc[1], $usersDesc[0]); + } }