Skip to content

Commit dd3f67d

Browse files
author
Jeremy Giberson
committed
updated manytomany so it maps field names to column names in criteria ordering
1 parent 0feaf92 commit dd3f67d

File tree

5 files changed

+121
-3
lines changed

5 files changed

+121
-3
lines changed

lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri
278278
. implode(' AND ', $onConditions)
279279
. ' WHERE ' . implode(' AND ', $whereClauses);
280280

281-
$sql .= $this->getOrderingSql($criteria);
281+
$sql .= $this->getOrderingSql($criteria, $targetClass);
282282

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

@@ -747,14 +747,20 @@ private function expandCriteriaParameters(Criteria $criteria)
747747

748748
/**
749749
* @param Criteria $criteria
750+
* @param ClassMetadata $targetClass
750751
* @return string
751752
*/
752-
private function getOrderingSql(Criteria $criteria)
753+
private function getOrderingSql(Criteria $criteria, ClassMetadata $targetClass)
753754
{
754755
$orderings = $criteria->getOrderings();
755756
if ($orderings) {
756757
$orderBy = [];
757-
foreach ($orderings as $field => $direction) {
758+
foreach ($orderings as $name => $direction) {
759+
$field = $this->quoteStrategy->getColumnName(
760+
$name,
761+
$targetClass,
762+
$this->platform
763+
);
758764
$orderBy[] = $field . ' ' . $direction;
759765
}
760766

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/*
3+
* To change this template, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
namespace Doctrine\Tests\Models\CMS;
8+
9+
/**
10+
* Description of CmsTag
11+
*
12+
* @Entity
13+
* @Table(name="cms_tags")
14+
*/
15+
class CmsTag
16+
{
17+
/**
18+
* @Id
19+
* @Column(type="integer")
20+
* @GeneratedValue
21+
*/
22+
public $id;
23+
/**
24+
* @Column(length=50, name="tag_name", nullable=true)
25+
*/
26+
public $name;
27+
/**
28+
* @ManyToMany(targetEntity="CmsUser", mappedBy="tags")
29+
*/
30+
public $users;
31+
32+
public function setName($name) {
33+
$this->name = $name;
34+
}
35+
36+
public function getName() {
37+
return $this->name;
38+
}
39+
40+
public function addUser(CmsUser $user) {
41+
$this->users[] = $user;
42+
}
43+
44+
public function getUsers() {
45+
return $this->users;
46+
}
47+
}
48+

tests/Doctrine/Tests/Models/CMS/CmsUser.php

+18
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ class CmsUser
162162
* )
163163
*/
164164
public $groups;
165+
/**
166+
* @ManyToMany(targetEntity="CmsTag", inversedBy="users", cascade={"persist", "merge", "detach"})
167+
* @JoinTable(name="cms_users_tags",
168+
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
169+
* inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}
170+
* )
171+
*/
172+
public $tags;
165173

166174
public $nonPersistedProperty;
167175

@@ -171,6 +179,7 @@ public function __construct() {
171179
$this->phonenumbers = new ArrayCollection;
172180
$this->articles = new ArrayCollection;
173181
$this->groups = new ArrayCollection;
182+
$this->tags = new ArrayCollection;
174183
}
175184

176185
public function getId() {
@@ -217,6 +226,15 @@ public function getGroups() {
217226
return $this->groups;
218227
}
219228

229+
public function addTag(CmsTag $tag) {
230+
$this->tags[] = $tag;
231+
$tag->addUser($this);
232+
}
233+
234+
public function getTags() {
235+
return $this->tags;
236+
}
237+
220238
public function removePhonenumber($index) {
221239
if (isset($this->phonenumbers[$index])) {
222240
$ph = $this->phonenumbers[$index];

tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Doctrine\Tests\ORM\Functional;
44

55
use Doctrine\Common\Collections\Criteria;
6+
use Doctrine\Tests\Models\CMS\CmsTag;
67
use Doctrine\Tests\Models\CMS\CmsUser,
78
Doctrine\Tests\Models\CMS\CmsGroup,
89
Doctrine\Common\Collections\ArrayCollection;
@@ -418,6 +419,50 @@ public function testManyToManyOrderByIsNotIgnored()
418419
);
419420
}
420421

422+
/**
423+
* @group DDC-3952
424+
*/
425+
public function testManyToManyOrderByHonorsFieldNameColumnNameAliases()
426+
{
427+
$user = new CmsUser;
428+
$user->name = 'Guilherme';
429+
$user->username = 'gblanco';
430+
$user->status = 'developer';
431+
432+
$tag1 = new CmsTag;
433+
$tag2 = new CmsTag;
434+
$tag3 = new CmsTag;
435+
436+
$tag1->name = 'C';
437+
$tag2->name = 'A';
438+
$tag3->name = 'B';
439+
440+
$user->addTag($tag1);
441+
$user->addTag($tag2);
442+
$user->addTag($tag3);
443+
444+
$this->_em->persist($user);
445+
$this->_em->flush();
446+
447+
$this->_em->clear();
448+
449+
$user = $this->_em->find(get_class($user), $user->id);
450+
451+
$criteria = Criteria::create()
452+
->orderBy(['name' => Criteria::ASC]);
453+
454+
$this->assertEquals(
455+
['A', 'B', 'C'],
456+
$user
457+
->getTags()
458+
->matching($criteria)
459+
->map(function (CmsTag $tag) {
460+
return $tag->getName();
461+
})
462+
->toArray()
463+
);
464+
}
465+
421466
public function testMatchingWithLimit()
422467
{
423468
$user = $this->addCmsUserGblancoWithGroups(2);

tests/Doctrine/Tests/OrmFunctionalTestCase.php

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
8383
'Doctrine\Tests\Models\CMS\CmsAddress',
8484
'Doctrine\Tests\Models\CMS\CmsEmail',
8585
'Doctrine\Tests\Models\CMS\CmsGroup',
86+
'Doctrine\Tests\Models\CMS\CmsTag',
8687
'Doctrine\Tests\Models\CMS\CmsArticle',
8788
'Doctrine\Tests\Models\CMS\CmsComment',
8889
),

0 commit comments

Comments
 (0)