1
0
mirror of synced 2025-02-02 21:41:45 +03:00

updated manytomany so it maps field names to column names in criteria ordering

This commit is contained in:
Jeremy Giberson 2016-02-11 13:39:31 -07:00
parent 0feaf92348
commit dd3f67d862
5 changed files with 121 additions and 3 deletions

View File

@ -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;
}

View File

@ -0,0 +1,48 @@
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
namespace Doctrine\Tests\Models\CMS;
/**
* Description of CmsTag
*
* @Entity
* @Table(name="cms_tags")
*/
class CmsTag
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
public $id;
/**
* @Column(length=50, name="tag_name", nullable=true)
*/
public $name;
/**
* @ManyToMany(targetEntity="CmsUser", mappedBy="tags")
*/
public $users;
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
public function addUser(CmsUser $user) {
$this->users[] = $user;
}
public function getUsers() {
return $this->users;
}
}

View File

@ -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];

View File

@ -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);

View File

@ -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',
),