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\CoreBundle\Doctrine\ORM; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityManager; |
15
|
|
|
use Doctrine\ORM\Mapping; |
16
|
|
|
use Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductRepository as BaseProductRepository; |
17
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
18
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
19
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
23
|
|
|
* @author Gonzalo Vilaseca <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ProductRepository extends BaseProductRepository implements ProductRepositoryInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var AssociationHydrator |
29
|
|
|
*/ |
30
|
|
|
private $associationHydrator; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param EntityManager $em |
34
|
|
|
* @param Mapping\ClassMetadata $class |
35
|
|
|
*/ |
36
|
|
|
public function __construct(EntityManager $em, Mapping\ClassMetadata $class) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
parent::__construct($em, $class); |
39
|
|
|
|
40
|
|
|
$this->associationHydrator = new AssociationHydrator($em, $class); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function createListQueryBuilder($locale, $taxonId = null) |
47
|
|
|
{ |
48
|
|
|
$queryBuilder = $this->createQueryBuilder('o') |
49
|
|
|
->addSelect('translation') |
50
|
|
|
->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale') |
51
|
|
|
->setParameter('locale', $locale) |
52
|
|
|
; |
53
|
|
|
|
54
|
|
|
if (null !== $taxonId) { |
55
|
|
|
$queryBuilder |
56
|
|
|
->innerJoin('o.productTaxons', 'productTaxon') |
57
|
|
|
->andWhere('productTaxon.taxon = :taxonId') |
58
|
|
|
->setParameter('taxonId', $taxonId) |
59
|
|
|
; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $queryBuilder; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function createShopListQueryBuilder(ChannelInterface $channel, TaxonInterface $taxon, $locale, array $sorting) |
69
|
|
|
{ |
70
|
|
|
$queryBuilder = $this->createQueryBuilder('o') |
71
|
|
|
->addSelect('translation') |
72
|
|
|
->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale') |
73
|
|
|
->innerJoin('o.productTaxons', 'productTaxon') |
74
|
|
|
->andWhere('productTaxon.taxon = :taxon') |
75
|
|
|
->andWhere(':channel MEMBER OF o.channels') |
76
|
|
|
->andWhere('o.enabled = true') |
77
|
|
|
->addGroupBy('o.id') |
78
|
|
|
->setParameter('locale', $locale) |
79
|
|
|
->setParameter('taxon', $taxon) |
80
|
|
|
->setParameter('channel', $channel) |
81
|
|
|
; |
82
|
|
|
|
83
|
|
|
// Grid hack, we do not need to join these if we don't sort by price |
84
|
|
|
if (isset($sorting['price'])) { |
85
|
|
|
$queryBuilder |
86
|
|
|
->innerJoin('o.variants', 'variant') |
87
|
|
|
->innerJoin('variant.channelPricings', 'channelPricing') |
88
|
|
|
->andWhere('channelPricing.channel = :channel') |
89
|
|
|
; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $queryBuilder; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function findLatestByChannel(ChannelInterface $channel, $locale, $count) |
99
|
|
|
{ |
100
|
|
|
return $this->createQueryBuilder('o') |
101
|
|
|
->addSelect('translation') |
102
|
|
|
->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale') |
103
|
|
|
->andWhere(':channel MEMBER OF o.channels') |
104
|
|
|
->andWhere('o.enabled = true') |
105
|
|
|
->addOrderBy('o.createdAt', 'DESC') |
106
|
|
|
->setParameter('channel', $channel) |
107
|
|
|
->setParameter('locale', $locale) |
108
|
|
|
->setMaxResults($count) |
109
|
|
|
->getQuery() |
110
|
|
|
->getResult() |
111
|
|
|
; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function findOneByChannelAndSlug(ChannelInterface $channel, $locale, $slug) |
118
|
|
|
{ |
119
|
|
|
$product = $this->createQueryBuilder('o') |
120
|
|
|
->addSelect('translation') |
121
|
|
|
->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale') |
122
|
|
|
->andWhere('translation.slug = :slug') |
123
|
|
|
->andWhere(':channel MEMBER OF o.channels') |
124
|
|
|
->andWhere('o.enabled = true') |
125
|
|
|
->setParameter('channel', $channel) |
126
|
|
|
->setParameter('locale', $locale) |
127
|
|
|
->setParameter('slug', $slug) |
128
|
|
|
->getQuery() |
129
|
|
|
->getOneOrNullResult() |
130
|
|
|
; |
131
|
|
|
|
132
|
|
|
$this->associationHydrator->hydrateAssociations($product, [ |
133
|
|
|
'images', |
134
|
|
|
'options', |
135
|
|
|
'options.translations', |
136
|
|
|
'variants.channelPricings', |
137
|
|
|
'variants.optionValues', |
138
|
|
|
'variants.optionValues.translations', |
139
|
|
|
]); |
140
|
|
|
|
141
|
|
|
return $product; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function findOneByCode($code) |
148
|
|
|
{ |
149
|
|
|
return $this->createQueryBuilder('o') |
150
|
|
|
->where('o.code = :code') |
151
|
|
|
->setParameter('code', $code) |
152
|
|
|
->getQuery() |
153
|
|
|
->getOneOrNullResult() |
154
|
|
|
; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.