1
0
Fork 0
mirror of synced 2025-04-03 13:23:37 +03:00

[DDC-1094] Add support for limit, offset and orderby in EntityRepository::findBy().

This commit is contained in:
Benjamin Eberlei 2011-04-30 23:18:24 +02:00
parent 0b1077a3b3
commit 73c7605a5c
5 changed files with 58 additions and 17 deletions

View file

@ -160,11 +160,14 @@ class EntityRepository implements ObjectRepository
* Finds entities by a set of criteria. * Finds entities by a set of criteria.
* *
* @param array $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);
} }
/** /**

View file

@ -680,12 +680,15 @@ class BasicEntityPersister
* Loads a list of entities by a list of field criteria. * Loads a list of entities by a list of field criteria.
* *
* @param array $criteria * @param array $criteria
* @param array $orderBy
* @param int $limit
* @param int $offset
* @return array * @return array
*/ */
public function loadAll(array $criteria = array()) public function loadAll(array $criteria = array(), array $orderBy = null, $limit = null, $offset = null)
{ {
$entities = array(); $entities = array();
$sql = $this->_getSelectEntitiesSQL($criteria); $sql = $this->_getSelectEntitiesSQL($criteria, null, 0, $limit, $offset, $orderBy);
list($params, $types) = $this->expandParameters($criteria); list($params, $types) = $this->expandParameters($criteria);
$stmt = $this->_conn->executeQuery($sql, $params, $types); $stmt = $this->_conn->executeQuery($sql, $params, $types);
@ -831,19 +834,21 @@ class BasicEntityPersister
* @param AssociationMapping $assoc * @param AssociationMapping $assoc
* @param string $orderBy * @param string $orderBy
* @param int $lockMode * @param int $lockMode
* @param int $limit
* @param int $offset
* @param array $orderBy
* @return string * @return string
* @todo Refactor: _getSelectSQL(...) * @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 ? $joinSql = $assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY ?
$this->_getSelectManyToManyJoinSQL($assoc) : ''; $this->_getSelectManyToManyJoinSQL($assoc) : '';
$conditionSql = $this->_getSelectConditionSQL($criteria, $assoc); $conditionSql = $this->_getSelectConditionSQL($criteria, $assoc);
$orderBySql = $assoc !== null && isset($assoc['orderBy']) ? $orderBy = ($assoc !== null && isset($assoc['orderBy'])) ? $assoc['orderBy'] : $orderBy;
$this->_getCollectionOrderBySQL($assoc['orderBy'], $this->_getSQLTableAlias($this->_class->name)) $orderBySql = $orderBy ? $this->_getOrderBySQL($orderBy, $this->_getSQLTableAlias($this->_class->name)) : '';
: '';
$lockSql = ''; $lockSql = '';
if ($lockMode == LockMode::PESSIMISTIC_READ) { if ($lockMode == LockMode::PESSIMISTIC_READ) {
@ -869,12 +874,12 @@ class BasicEntityPersister
* @return string * @return string
* @todo Rename: _getOrderBySQL * @todo Rename: _getOrderBySQL
*/ */
protected final function _getCollectionOrderBySQL(array $orderBy, $baseTableAlias) protected final function _getOrderBySQL(array $orderBy, $baseTableAlias)
{ {
$orderBySql = ''; $orderBySql = '';
foreach ($orderBy as $fieldName => $orientation) { foreach ($orderBy as $fieldName => $orientation) {
if ( ! isset($this->_class->fieldMappings[$fieldName])) { if ( ! isset($this->_class->fieldMappings[$fieldName])) {
ORMException::unrecognizedField($fieldName); throw ORMException::unrecognizedField($fieldName);
} }
$tableAlias = isset($this->_class->fieldMappings[$fieldName]['inherited']) ? $tableAlias = isset($this->_class->fieldMappings[$fieldName]['inherited']) ?

View file

@ -238,7 +238,7 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
/** /**
* {@inheritdoc} * {@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(); $idColumns = $this->_class->getIdentifierColumnNames();
$baseTableAlias = $this->_getSQLTableAlias($this->_class->name); $baseTableAlias = $this->_getSQLTableAlias($this->_class->name);
@ -343,10 +343,8 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
$conditionSql = $this->_getSelectConditionSQL($criteria, $assoc); $conditionSql = $this->_getSelectConditionSQL($criteria, $assoc);
$orderBySql = ''; $orderBy = ($assoc !== null && isset($assoc['orderBy'])) ? $assoc['orderBy'] : $orderBy;
if ($assoc != null && isset($assoc['orderBy'])) { $orderBySql = $orderBy ? $this->_getOrderBySQL($orderBy, $baseTableAlias) : '';
$orderBySql = $this->_getCollectionOrderBySQL($assoc['orderBy'], $baseTableAlias);
}
if ($this->_selectColumnListSql === null) { if ($this->_selectColumnListSql === null) {
$this->_selectColumnListSql = $columnList; $this->_selectColumnListSql = $columnList;

@ -1 +1 @@
Subproject commit ba63ae0f0b6b62a2a8617f01386698730ff2b713 Subproject commit 076a03f8f40b6e08f0ae2f4ee2678474e64b6f59

View file

@ -320,5 +320,40 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertEquals(1, count($params), "Should only execute with one parameter."); $this->assertEquals(1, count($params), "Should only execute with one parameter.");
$this->assertEquals(array('romanb'), $params); $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]);
}
} }