diff --git a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php index cb620210b..25c03a450 100644 --- a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php @@ -278,7 +278,7 @@ class ManyToManyPersister extends AbstractCollectionPersister . implode(' AND ', $onConditions) . ' WHERE ' . implode(' AND ', $whereClauses); - $sql .= $this->getOrderingSql($criteria); + $sql .= $this->getOrderingSql($criteria, $targetClass); $sql .= $this->getLimitSql($criteria); @@ -747,14 +747,20 @@ class ManyToManyPersister extends AbstractCollectionPersister /** * @param Criteria $criteria + * @param ClassMetadata $targetClass * @return string */ - private function getOrderingSql(Criteria $criteria) + private function getOrderingSql(Criteria $criteria, ClassMetadata $targetClass) { $orderings = $criteria->getOrderings(); if ($orderings) { $orderBy = []; - foreach ($orderings as $field => $direction) { + foreach ($orderings as $name => $direction) { + $field = $this->quoteStrategy->getColumnName( + $name, + $targetClass, + $this->platform + ); $orderBy[] = $field . ' ' . $direction; } diff --git a/tests/Doctrine/Tests/Models/CMS/CmsTag.php b/tests/Doctrine/Tests/Models/CMS/CmsTag.php new file mode 100644 index 000000000..cf7486cb0 --- /dev/null +++ b/tests/Doctrine/Tests/Models/CMS/CmsTag.php @@ -0,0 +1,48 @@ +name = $name; + } + + public function getName() { + return $this->name; + } + + public function addUser(CmsUser $user) { + $this->users[] = $user; + } + + public function getUsers() { + return $this->users; + } +} + diff --git a/tests/Doctrine/Tests/Models/CMS/CmsUser.php b/tests/Doctrine/Tests/Models/CMS/CmsUser.php index c95cce4ea..468c71220 100644 --- a/tests/Doctrine/Tests/Models/CMS/CmsUser.php +++ b/tests/Doctrine/Tests/Models/CMS/CmsUser.php @@ -162,6 +162,14 @@ class CmsUser * ) */ public $groups; + /** + * @ManyToMany(targetEntity="CmsTag", inversedBy="users", cascade={"persist", "merge", "detach"}) + * @JoinTable(name="cms_users_tags", + * joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")}, + * inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")} + * ) + */ + public $tags; public $nonPersistedProperty; @@ -171,6 +179,7 @@ class CmsUser $this->phonenumbers = new ArrayCollection; $this->articles = new ArrayCollection; $this->groups = new ArrayCollection; + $this->tags = new ArrayCollection; } public function getId() { @@ -217,6 +226,15 @@ class CmsUser return $this->groups; } + public function addTag(CmsTag $tag) { + $this->tags[] = $tag; + $tag->addUser($this); + } + + public function getTags() { + return $this->tags; + } + public function removePhonenumber($index) { if (isset($this->phonenumbers[$index])) { $ph = $this->phonenumbers[$index]; diff --git a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php index b4c10620d..da4d30077 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php @@ -3,6 +3,7 @@ namespace Doctrine\Tests\ORM\Functional; use Doctrine\Common\Collections\Criteria; +use Doctrine\Tests\Models\CMS\CmsTag; use Doctrine\Tests\Models\CMS\CmsUser, Doctrine\Tests\Models\CMS\CmsGroup, Doctrine\Common\Collections\ArrayCollection; @@ -418,6 +419,50 @@ class ManyToManyBasicAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCa ); } + /** + * @group DDC-3952 + */ + public function testManyToManyOrderByHonorsFieldNameColumnNameAliases() + { + $user = new CmsUser; + $user->name = 'Guilherme'; + $user->username = 'gblanco'; + $user->status = 'developer'; + + $tag1 = new CmsTag; + $tag2 = new CmsTag; + $tag3 = new CmsTag; + + $tag1->name = 'C'; + $tag2->name = 'A'; + $tag3->name = 'B'; + + $user->addTag($tag1); + $user->addTag($tag2); + $user->addTag($tag3); + + $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]); + + $this->assertEquals( + ['A', 'B', 'C'], + $user + ->getTags() + ->matching($criteria) + ->map(function (CmsTag $tag) { + return $tag->getName(); + }) + ->toArray() + ); + } + public function testMatchingWithLimit() { $user = $this->addCmsUserGblancoWithGroups(2); diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index b8a705707..f19e19ff9 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -83,6 +83,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase 'Doctrine\Tests\Models\CMS\CmsAddress', 'Doctrine\Tests\Models\CMS\CmsEmail', 'Doctrine\Tests\Models\CMS\CmsGroup', + 'Doctrine\Tests\Models\CMS\CmsTag', 'Doctrine\Tests\Models\CMS\CmsArticle', 'Doctrine\Tests\Models\CMS\CmsComment', ),