updated manytomany so it maps field names to column names in criteria ordering
This commit is contained in:
parent
0feaf92348
commit
dd3f67d862
@ -278,7 +278,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
|||||||
. implode(' AND ', $onConditions)
|
. implode(' AND ', $onConditions)
|
||||||
. ' WHERE ' . implode(' AND ', $whereClauses);
|
. ' WHERE ' . implode(' AND ', $whereClauses);
|
||||||
|
|
||||||
$sql .= $this->getOrderingSql($criteria);
|
$sql .= $this->getOrderingSql($criteria, $targetClass);
|
||||||
|
|
||||||
$sql .= $this->getLimitSql($criteria);
|
$sql .= $this->getLimitSql($criteria);
|
||||||
|
|
||||||
@ -747,14 +747,20 @@ class ManyToManyPersister extends AbstractCollectionPersister
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Criteria $criteria
|
* @param Criteria $criteria
|
||||||
|
* @param ClassMetadata $targetClass
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function getOrderingSql(Criteria $criteria)
|
private function getOrderingSql(Criteria $criteria, ClassMetadata $targetClass)
|
||||||
{
|
{
|
||||||
$orderings = $criteria->getOrderings();
|
$orderings = $criteria->getOrderings();
|
||||||
if ($orderings) {
|
if ($orderings) {
|
||||||
$orderBy = [];
|
$orderBy = [];
|
||||||
foreach ($orderings as $field => $direction) {
|
foreach ($orderings as $name => $direction) {
|
||||||
|
$field = $this->quoteStrategy->getColumnName(
|
||||||
|
$name,
|
||||||
|
$targetClass,
|
||||||
|
$this->platform
|
||||||
|
);
|
||||||
$orderBy[] = $field . ' ' . $direction;
|
$orderBy[] = $field . ' ' . $direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
48
tests/Doctrine/Tests/Models/CMS/CmsTag.php
Normal file
48
tests/Doctrine/Tests/Models/CMS/CmsTag.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -162,6 +162,14 @@ class CmsUser
|
|||||||
* )
|
* )
|
||||||
*/
|
*/
|
||||||
public $groups;
|
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;
|
public $nonPersistedProperty;
|
||||||
|
|
||||||
@ -171,6 +179,7 @@ class CmsUser
|
|||||||
$this->phonenumbers = new ArrayCollection;
|
$this->phonenumbers = new ArrayCollection;
|
||||||
$this->articles = new ArrayCollection;
|
$this->articles = new ArrayCollection;
|
||||||
$this->groups = new ArrayCollection;
|
$this->groups = new ArrayCollection;
|
||||||
|
$this->tags = new ArrayCollection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getId() {
|
public function getId() {
|
||||||
@ -217,6 +226,15 @@ class CmsUser
|
|||||||
return $this->groups;
|
return $this->groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addTag(CmsTag $tag) {
|
||||||
|
$this->tags[] = $tag;
|
||||||
|
$tag->addUser($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTags() {
|
||||||
|
return $this->tags;
|
||||||
|
}
|
||||||
|
|
||||||
public function removePhonenumber($index) {
|
public function removePhonenumber($index) {
|
||||||
if (isset($this->phonenumbers[$index])) {
|
if (isset($this->phonenumbers[$index])) {
|
||||||
$ph = $this->phonenumbers[$index];
|
$ph = $this->phonenumbers[$index];
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace Doctrine\Tests\ORM\Functional;
|
namespace Doctrine\Tests\ORM\Functional;
|
||||||
|
|
||||||
use Doctrine\Common\Collections\Criteria;
|
use Doctrine\Common\Collections\Criteria;
|
||||||
|
use Doctrine\Tests\Models\CMS\CmsTag;
|
||||||
use Doctrine\Tests\Models\CMS\CmsUser,
|
use Doctrine\Tests\Models\CMS\CmsUser,
|
||||||
Doctrine\Tests\Models\CMS\CmsGroup,
|
Doctrine\Tests\Models\CMS\CmsGroup,
|
||||||
Doctrine\Common\Collections\ArrayCollection;
|
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()
|
public function testMatchingWithLimit()
|
||||||
{
|
{
|
||||||
$user = $this->addCmsUserGblancoWithGroups(2);
|
$user = $this->addCmsUserGblancoWithGroups(2);
|
||||||
|
@ -83,6 +83,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
|
|||||||
'Doctrine\Tests\Models\CMS\CmsAddress',
|
'Doctrine\Tests\Models\CMS\CmsAddress',
|
||||||
'Doctrine\Tests\Models\CMS\CmsEmail',
|
'Doctrine\Tests\Models\CMS\CmsEmail',
|
||||||
'Doctrine\Tests\Models\CMS\CmsGroup',
|
'Doctrine\Tests\Models\CMS\CmsGroup',
|
||||||
|
'Doctrine\Tests\Models\CMS\CmsTag',
|
||||||
'Doctrine\Tests\Models\CMS\CmsArticle',
|
'Doctrine\Tests\Models\CMS\CmsArticle',
|
||||||
'Doctrine\Tests\Models\CMS\CmsComment',
|
'Doctrine\Tests\Models\CMS\CmsComment',
|
||||||
),
|
),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user