Skip to content

Commit

Permalink
Merge branch 'fix-many-many-criteria' of github.com:petitchevalroux/d…
Browse files Browse the repository at this point in the history
…octrine2 into many-to-many-criteria-fixes

Conflicts:
	lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php
  • Loading branch information
Jeremy Giberson committed Feb 11, 2016
2 parents 0adeade + cfeda90 commit ae78575
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri

$sql .= $this->getOrderingSql($criteria);

$sql .= $this->getLimitSql($criteria);

$stmt = $this->conn->executeQuery($sql, $params);

return $this
Expand Down Expand Up @@ -760,4 +762,19 @@ private function getOrderingSql(Criteria $criteria)
}
return '';
}

/**
* @param Criteria $criteria
* @return string
* @throws \Doctrine\DBAL\DBALException
*/
private function getLimitSql(Criteria $criteria)
{
$limit = $criteria->getMaxResults();
$offset = $criteria->getFirstResult();
if ($limit !== null || $offset !== null) {
return $this->platform->modifyLimitQuery('', $limit, $offset);
}
return '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,69 @@ public function testManyToManyOrderByIsNotIgnored()
$this->assertEquals(['A', 'B', 'C', 'Developers_0'], $existingOrder);
}

public function testMatchingWithLimit()
{
$user = $this->addCmsUserGblancoWithGroups(2);
$this->_em->clear();

$user = $this->_em->find(get_class($user), $user->id);

$groups = $user->groups;
$this->assertFalse($user->groups->isInitialized(), "Pre-condition: lazy collection");

$criteria = Criteria::create()->setMaxResults(1);
$result = $groups->matching($criteria);

$this->assertCount(1, $result);

$this->assertFalse($user->groups->isInitialized(), "Post-condition: matching does not initialize collection");
}

public function testMatchingWithOffset()
{
$user = $this->addCmsUserGblancoWithGroups(2);
$this->_em->clear();

$user = $this->_em->find(get_class($user), $user->id);

$groups = $user->groups;
$this->assertFalse($user->groups->isInitialized(), "Pre-condition: lazy collection");

$criteria = Criteria::create()->setFirstResult(1);
$result = $groups->matching($criteria);

$this->assertCount(1, $result);

$firstGroup = $result->first();
$this->assertEquals('Developers_1', $firstGroup->name);

$this->assertFalse($user->groups->isInitialized(), "Post-condition: matching does not initialize collection");
}

public function testMatchingWithLimitAndOffset()
{
$user = $this->addCmsUserGblancoWithGroups(5);
$this->_em->clear();

$user = $this->_em->find(get_class($user), $user->id);

$groups = $user->groups;
$this->assertFalse($user->groups->isInitialized(), "Pre-condition: lazy collection");

$criteria = Criteria::create()->setFirstResult(1)->setMaxResults(3);
$result = $groups->matching($criteria);

$this->assertCount(3, $result);

$firstGroup = $result->first();
$this->assertEquals('Developers_1', $firstGroup->name);

$lastGroup = $result->last();
$this->assertEquals('Developers_3', $lastGroup->name);

$this->assertFalse($user->groups->isInitialized(), "Post-condition: matching does not initialize collection");
}

public function testMatching()
{
$user = $this->addCmsUserGblancoWithGroups(2);
Expand Down

0 comments on commit ae78575

Please sign in to comment.