diff --git a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php index f476f2d0c..eeebc7b8a 100644 --- a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php @@ -278,6 +278,8 @@ class ManyToManyPersister extends AbstractCollectionPersister . implode(' AND ', $onConditions) . ' WHERE ' . implode(' AND ', $whereClauses); + $sql .= $this->getOrderingSql($criteria); + $stmt = $this->conn->executeQuery($sql, $params); return $this @@ -740,4 +742,22 @@ class ManyToManyPersister extends AbstractCollectionPersister return $types; } + + /** + * @param Criteria $criteria + * @return string + */ + private function getOrderingSql(Criteria $criteria) + { + $orderings = $criteria->getOrderings(); + if ($orderings) { + $orderBy = []; + foreach ($orderings as $field => $direction) { + $orderBy[] = $field . ' ' . $direction; + } + + return ' ORDER BY ' . implode(', ', $orderBy); + } + return ''; + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php index e6e42f0a0..9a020a8aa 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php @@ -377,6 +377,44 @@ class ManyToManyBasicAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCa $this->assertEquals(0, count($user->groups)); } + /** + * @group DDC-3952 + */ + public function testManyToManyOrderByIsNotIgnored() + { + $user = $this->addCmsUserGblancoWithGroups(1); + + $group = new CmsGroup; + $group->name = 'C'; + $user->addGroup($group); + + $group = new CmsGroup; + $group->name = 'A'; + $user->addGroup($group); + + $group = new CmsGroup; + $group->name = 'B'; + $user->addGroup($group); + + $this->_em->persist($user); + $this->_em->flush(); + + $this->_em->clear(); + + $user = $this->_em->find(get_class($user), $user->id); + + $criteria = Criteria::create() + ->orderBy(['name' => Criteria::ASC]); + $groups = $user->getGroups()->matching($criteria); + + $existingOrder = []; + foreach ($groups as $group) { + $existingOrder[] = $group->getName(); + } + + $this->assertEquals(['A', 'B', 'C', 'Developers_0'], $existingOrder); + } + public function testMatching() { $user = $this->addCmsUserGblancoWithGroups(2);