-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7766 from stephanschuler/respect-collection-order…
…ing-in-selectable-matching Respect collection orderBy meta when matching() Fixes #7767
- Loading branch information
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
tests/Doctrine/Tests/ORM/Functional/Ticket/GH7767Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket; | ||
|
||
use Doctrine\Common\Collections\Criteria; | ||
use Doctrine\ORM\PersistentCollection; | ||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
use function assert; | ||
|
||
/** | ||
* @group GH7767 | ||
*/ | ||
class GH7767Test extends OrmFunctionalTestCase | ||
{ | ||
protected function setUp() : void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->setUpEntitySchema([GH7767ParentEntity::class, GH7767ChildEntity::class]); | ||
|
||
$parent = new GH7767ParentEntity(); | ||
$parent->addChild(200); | ||
$parent->addChild(100); | ||
$parent->addChild(300); | ||
|
||
$this->_em->persist($parent); | ||
$this->_em->flush(); | ||
$this->_em->clear(); | ||
} | ||
|
||
public function testMatchingRespectsCollectionOrdering() : void | ||
{ | ||
$parent = $this->_em->find(GH7767ParentEntity::class, 1); | ||
assert($parent instanceof GH7767ParentEntity); | ||
|
||
$children = $parent->getChildren()->matching(Criteria::create()); | ||
|
||
self::assertEquals(100, $children[0]->position); | ||
self::assertEquals(200, $children[1]->position); | ||
self::assertEquals(300, $children[2]->position); | ||
} | ||
|
||
public function testMatchingOverrulesCollectionOrdering() : void | ||
{ | ||
$parent = $this->_em->find(GH7767ParentEntity::class, 1); | ||
assert($parent instanceof GH7767ParentEntity); | ||
|
||
$children = $parent->getChildren()->matching(Criteria::create()->orderBy(['position' => 'DESC'])); | ||
|
||
self::assertEquals(300, $children[0]->position); | ||
self::assertEquals(200, $children[1]->position); | ||
self::assertEquals(100, $children[2]->position); | ||
} | ||
} | ||
|
||
/** | ||
* @Entity | ||
*/ | ||
class GH7767ParentEntity | ||
{ | ||
/** | ||
* @Id | ||
* @Column(type="integer") | ||
* @GeneratedValue | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @OneToMany(targetEntity=GH7767ChildEntity::class, mappedBy="parent", fetch="EXTRA_LAZY", cascade={"persist"}) | ||
* @OrderBy({"position" = "ASC"}) | ||
*/ | ||
private $children; | ||
|
||
public function addChild(int $position) : void | ||
{ | ||
$this->children[] = new GH7767ChildEntity($this, $position); | ||
} | ||
|
||
public function getChildren() : PersistentCollection | ||
{ | ||
return $this->children; | ||
} | ||
} | ||
|
||
/** | ||
* @Entity | ||
*/ | ||
class GH7767ChildEntity | ||
{ | ||
/** | ||
* @Id | ||
* @Column(type="integer") | ||
* @GeneratedValue | ||
*/ | ||
private $id; | ||
|
||
/** @Column(type="integer") */ | ||
public $position; | ||
|
||
/** @ManyToOne(targetEntity=GH7767ParentEntity::class, inversedBy="children") */ | ||
private $parent; | ||
|
||
public function __construct(GH7767ParentEntity $parent, int $position) | ||
{ | ||
$this->parent = $parent; | ||
$this->position = $position; | ||
} | ||
} |