Skip to content

Commit

Permalink
Merge pull request #7766 from stephanschuler/respect-collection-order…
Browse files Browse the repository at this point in the history
…ing-in-selectable-matching

Respect collection orderBy meta when matching()

Fixes #7767
  • Loading branch information
lcobucci authored Aug 13, 2019
2 parents 6e56bcd + e51666e commit 6b7f53f
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Doctrine\Common\Collections\Selectable;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping\ClassMetadata;
use function array_merge;
use function get_class;

/**
Expand Down Expand Up @@ -671,6 +672,7 @@ public function matching(Criteria $criteria)

$criteria = clone $criteria;
$criteria->where($expression);
$criteria->orderBy(array_merge($this->association['orderBy'] ?? [], $criteria->getOrderings()));

$persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']);

Expand Down
110 changes: 110 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH7767Test.php
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;
}
}

0 comments on commit 6b7f53f

Please sign in to comment.