1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Functional\Ticket; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\Criteria; |
8
|
|
|
use Doctrine\ORM\PersistentCollection; |
9
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
10
|
|
|
use function assert; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @group GH7767 |
14
|
|
|
*/ |
15
|
|
|
class GH7767Test extends OrmFunctionalTestCase |
16
|
|
|
{ |
17
|
|
|
protected function setUp() : void |
18
|
|
|
{ |
19
|
|
|
parent::setUp(); |
20
|
|
|
|
21
|
|
|
$this->setUpEntitySchema([GH7767ParentEntity::class, GH7767ChildEntity::class]); |
22
|
|
|
|
23
|
|
|
$parent = new GH7767ParentEntity(); |
24
|
|
|
$parent->addChild(200); |
25
|
|
|
$parent->addChild(100); |
26
|
|
|
$parent->addChild(300); |
27
|
|
|
|
28
|
|
|
$this->_em->persist($parent); |
29
|
|
|
$this->_em->flush(); |
30
|
|
|
$this->_em->clear(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testMatchingRespectsCollectionOrdering() : void |
34
|
|
|
{ |
35
|
|
|
$parent = $this->_em->find(GH7767ParentEntity::class, 1); |
36
|
|
|
assert($parent instanceof GH7767ParentEntity); |
37
|
|
|
|
38
|
|
|
$children = $parent->getChildren()->matching(Criteria::create()); |
39
|
|
|
|
40
|
|
|
self::assertEquals(100, $children[0]->position); |
41
|
|
|
self::assertEquals(200, $children[1]->position); |
42
|
|
|
self::assertEquals(300, $children[2]->position); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testMatchingOverrulesCollectionOrdering() : void |
46
|
|
|
{ |
47
|
|
|
$parent = $this->_em->find(GH7767ParentEntity::class, 1); |
48
|
|
|
assert($parent instanceof GH7767ParentEntity); |
49
|
|
|
|
50
|
|
|
$children = $parent->getChildren()->matching(Criteria::create()->orderBy(['position' => 'DESC'])); |
51
|
|
|
|
52
|
|
|
self::assertEquals(300, $children[0]->position); |
53
|
|
|
self::assertEquals(200, $children[1]->position); |
54
|
|
|
self::assertEquals(100, $children[2]->position); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @Entity |
60
|
|
|
*/ |
61
|
|
|
class GH7767ParentEntity |
62
|
|
|
{ |
63
|
|
|
/** |
64
|
|
|
* @Id |
65
|
|
|
* @Column(type="integer") |
66
|
|
|
* @GeneratedValue |
67
|
|
|
*/ |
68
|
|
|
private $id; |
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @OneToMany(targetEntity=GH7767ChildEntity::class, mappedBy="parent", fetch="EXTRA_LAZY", cascade={"persist"}) |
72
|
|
|
* @OrderBy({"position" = "ASC"}) |
73
|
|
|
*/ |
74
|
|
|
private $children; |
75
|
|
|
|
76
|
|
|
public function addChild(int $position) : void |
77
|
|
|
{ |
78
|
|
|
$this->children[] = new GH7767ChildEntity($this, $position); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getChildren() : PersistentCollection |
82
|
|
|
{ |
83
|
|
|
return $this->children; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @Entity |
89
|
|
|
*/ |
90
|
|
|
class GH7767ChildEntity |
91
|
|
|
{ |
92
|
|
|
/** |
93
|
|
|
* @Id |
94
|
|
|
* @Column(type="integer") |
95
|
|
|
* @GeneratedValue |
96
|
|
|
*/ |
97
|
|
|
private $id; |
98
|
|
|
|
99
|
|
|
/** @Column(type="integer") */ |
100
|
|
|
public $position; |
101
|
|
|
|
102
|
|
|
/** @ManyToOne(targetEntity=GH7767ParentEntity::class, inversedBy="children") */ |
103
|
|
|
private $parent; |
104
|
|
|
|
105
|
|
|
public function __construct(GH7767ParentEntity $parent, int $position) |
106
|
|
|
{ |
107
|
|
|
$this->parent = $parent; |
108
|
|
|
$this->position = $position; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|