1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\TaxonomyBundle\Doctrine\ORM; |
13
|
|
|
|
14
|
|
|
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; |
15
|
|
|
use Sylius\Component\Taxonomy\Model\TaxonInterface; |
16
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Aram Alipoor <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class TaxonRepository extends EntityRepository implements TaxonRepositoryInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function findChildren(TaxonInterface $taxon) |
27
|
|
|
{ |
28
|
|
|
$root = $taxon->isRoot() ? $taxon : $taxon->getRoot(); |
29
|
|
|
|
30
|
|
|
$queryBuilder = $this->createQueryBuilder('o'); |
31
|
|
|
$queryBuilder |
32
|
|
|
->andWhere($queryBuilder->expr()->eq('o.root', ':root')) |
33
|
|
|
->andWhere($queryBuilder->expr()->lt('o.right', ':right')) |
34
|
|
|
->andWhere($queryBuilder->expr()->gt('o.left', ':left')) |
35
|
|
|
->setParameter('root', $root) |
36
|
|
|
->setParameter('left', $taxon->getLeft()) |
37
|
|
|
->setParameter('right', $taxon->getRight()) |
38
|
|
|
; |
39
|
|
|
|
40
|
|
|
return $queryBuilder->getQuery()->getResult(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function findChildrenAsTree(TaxonInterface $taxon) |
47
|
|
|
{ |
48
|
|
|
$queryBuilder = $this->createQueryBuilder('o'); |
49
|
|
|
$queryBuilder |
50
|
|
|
->addSelect('translation') |
51
|
|
|
->leftJoin('o.translations', 'translation') |
52
|
|
|
->addSelect('children') |
53
|
|
|
->leftJoin('o.children', 'children') |
54
|
|
|
->andWhere('o.parent = :parent') |
55
|
|
|
->addOrderBy('o.root') |
56
|
|
|
->addOrderBy('o.position') |
57
|
|
|
->setParameter('parent', $taxon) |
58
|
|
|
; |
59
|
|
|
|
60
|
|
|
return $queryBuilder->getQuery()->getResult(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function findChildrenByRootCode($code) |
67
|
|
|
{ |
68
|
|
|
/** @var TaxonInterface|null $root */ |
69
|
|
|
$root = $this->findOneBy(['code' => $code]); |
70
|
|
|
|
71
|
|
|
if (null === $root) { |
72
|
|
|
return []; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->findChildren($root); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function findChildrenAsTreeByRootCode($code) |
82
|
|
|
{ |
83
|
|
|
/** @var TaxonInterface|null $root */ |
84
|
|
|
$root = $this->findOneBy(['code' => $code]); |
85
|
|
|
|
86
|
|
|
if (null === $root) { |
87
|
|
|
return []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->findChildrenAsTree($root); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function findOneBySlug($slug) |
97
|
|
|
{ |
98
|
|
|
return $this->createQueryBuilder('o') |
99
|
|
|
->addSelect('translation') |
100
|
|
|
->leftJoin('o.translations', 'translation') |
101
|
|
|
->where('translation.slug = :slug') |
102
|
|
|
->setParameter('slug', $slug) |
103
|
|
|
->getQuery() |
104
|
|
|
->getOneOrNullResult() |
105
|
|
|
; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function findByName($name, $locale) |
112
|
|
|
{ |
113
|
|
|
return $this->createQueryBuilder('o') |
114
|
|
|
->addSelect('translation') |
115
|
|
|
->leftJoin('o.translations', 'translation') |
116
|
|
|
->andWhere('translation.name = :name') |
117
|
|
|
->andWhere('translation.locale = :locale') |
118
|
|
|
->setParameter('name', $name) |
119
|
|
|
->setParameter('locale', $locale) |
120
|
|
|
->getQuery() |
121
|
|
|
->getResult() |
122
|
|
|
; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function findRootNodes() |
129
|
|
|
{ |
130
|
|
|
$queryBuilder = $this->createQueryBuilder('o'); |
131
|
|
|
$queryBuilder |
132
|
|
|
->andWhere($queryBuilder->expr()->isNull($this->getPropertyName('parent'))) |
133
|
|
|
->orderBy('o.position') |
134
|
|
|
; |
135
|
|
|
|
136
|
|
|
return $queryBuilder->getQuery()->getResult(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function findNodesTreeSorted() |
143
|
|
|
{ |
144
|
|
|
$queryBuilder = $this->createQueryBuilder('o'); |
145
|
|
|
$queryBuilder |
146
|
|
|
->orderBy('o.root') |
147
|
|
|
->addOrderBy('o.position') |
148
|
|
|
; |
149
|
|
|
|
150
|
|
|
return $queryBuilder->getQuery()->getResult(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function createListQueryBuilder() |
157
|
|
|
{ |
158
|
|
|
return $this->createQueryBuilder('o')->leftJoin('o.translations', 'translation'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|