1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Functional; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\Criteria; |
8
|
|
|
use Doctrine\ORM\Mapping\FetchMode; |
9
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceCategory; |
10
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
11
|
|
|
use function strstr; |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Tests a bidirectional one-to-one association mapping (without inheritance). |
15
|
|
|
*/ |
16
|
|
|
class OneToManySelfReferentialAssociationTest extends OrmFunctionalTestCase |
17
|
|
|
{ |
18
|
|
|
private $parent; |
19
|
|
|
private $firstChild; |
20
|
|
|
private $secondChild; |
21
|
|
|
private $thirdChild; |
22
|
|
|
|
23
|
|
|
protected function setUp() : void |
24
|
|
|
{ |
25
|
|
|
$this->useModelSet('ecommerce'); |
26
|
|
|
parent::setUp(); |
27
|
|
|
$this->parent = new ECommerceCategory(); |
28
|
|
|
$this->parent->setName('Programming languages books'); |
29
|
|
|
$this->firstChild = new ECommerceCategory(); |
30
|
|
|
$this->firstChild->setName('Java books'); |
31
|
|
|
$this->secondChild = new ECommerceCategory(); |
32
|
|
|
$this->secondChild->setName('Php books'); |
33
|
|
|
$this->thirdChild = new ECommerceCategory(); |
34
|
|
|
$this->thirdChild->setName('BBQ books'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testMatchingWithNoOrderingUsesDatabaseDefault() : void |
38
|
|
|
{ |
39
|
|
|
$this->createFixture(); |
40
|
|
|
|
41
|
|
|
$metadata = $this->em->getClassMetadata(ECommerceCategory::class); |
42
|
|
|
$metadata->getProperty('children')->setFetchMode(FetchMode::LAZY); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$query = $this->em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc'); |
45
|
|
|
$result = $query->getResult(); |
46
|
|
|
$parent = $result[0]; |
47
|
|
|
$children = $parent->getChildren()->matching(Criteria::create()); |
48
|
|
|
$this->assertEquals('Java books', $children[0]->getName()); |
49
|
|
|
$this->assertEquals('Php books', $children[1]->getName()); |
50
|
|
|
$this->assertEquals('BBQ books', $children[2]->getName()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testMatchingRespectsCollectionOrdering() : void |
54
|
|
|
{ |
55
|
|
|
$this->createFixture(); |
56
|
|
|
|
57
|
|
|
$metadata = $this->em->getClassMetadata(ECommerceCategory::class); |
58
|
|
|
$metadata->getProperty('children')->setFetchMode(FetchMode::LAZY); |
59
|
|
|
$metadata->getProperty('children')->setOrderBy(['name' => 'DESC']); |
60
|
|
|
|
61
|
|
|
$query = $this->em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc'); |
62
|
|
|
$result = $query->getResult(); |
63
|
|
|
$parent = $result[0]; |
64
|
|
|
$children = $parent->getChildren()->matching(Criteria::create()); |
65
|
|
|
$this->assertEquals('Php books', $children[0]->getName()); |
66
|
|
|
$this->assertEquals('Java books', $children[1]->getName()); |
67
|
|
|
$this->assertEquals('BBQ books', $children[2]->getName()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testMatchingOverrulesCollectionOrdering() : void |
71
|
|
|
{ |
72
|
|
|
$this->createFixture(); |
73
|
|
|
|
74
|
|
|
$metadata = $this->em->getClassMetadata(ECommerceCategory::class); |
75
|
|
|
$metadata->getProperty('children')->setFetchMode(FetchMode::LAZY); |
76
|
|
|
$metadata->getProperty('children')->setOrderBy(['name' => 'DESC']); |
77
|
|
|
|
78
|
|
|
$query = $this->em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc'); |
79
|
|
|
$result = $query->getResult(); |
80
|
|
|
$parent = $result[0]; |
81
|
|
|
$children = $parent->getChildren()->matching(Criteria::create()->orderBy(['name' => 'ASC'])); |
82
|
|
|
$this->assertEquals('BBQ books', $children[0]->getName()); |
83
|
|
|
$this->assertEquals('Java books', $children[1]->getName()); |
84
|
|
|
$this->assertEquals('Php books', $children[2]->getName()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function createFixture() |
88
|
|
|
{ |
89
|
|
|
$this->parent->addChild($this->firstChild); |
90
|
|
|
$this->parent->addChild($this->secondChild); |
91
|
|
|
$this->parent->addChild($this->thirdChild); |
92
|
|
|
$this->em->persist($this->parent); |
93
|
|
|
|
94
|
|
|
$this->em->flush(); |
95
|
|
|
$this->em->clear(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|