1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file was created by developers working at BitBag |
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace BitBag\SyliusElasticsearchPlugin\Repository; |
12
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityRepository; |
14
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
15
|
|
|
use Sylius\Component\Attribute\Model\AttributeInterface; |
16
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
17
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface as BaseTaxonRepositoryInterface; |
18
|
|
|
|
19
|
|
|
class TaxonRepository implements TaxonRepositoryInterface |
20
|
|
|
{ |
21
|
|
|
/** @var BaseTaxonRepositoryInterface|EntityRepository */ |
22
|
|
|
private $baseTaxonRepository; |
23
|
|
|
|
24
|
|
|
/** @var ProductRepositoryInterface|EntityRepository */ |
25
|
|
|
private $productRepository; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $productTaxonEntityClass; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
private $productAttributeEntityClass; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
BaseTaxonRepositoryInterface $baseTaxonRepository, |
35
|
|
|
ProductRepositoryInterface $productRepository, |
36
|
|
|
string $productTaxonEntityClass, |
37
|
|
|
string $productAttributeEntityClass |
38
|
|
|
) { |
39
|
|
|
$this->baseTaxonRepository = $baseTaxonRepository; |
40
|
|
|
$this->productRepository = $productRepository; |
41
|
|
|
$this->productTaxonEntityClass = $productTaxonEntityClass; |
42
|
|
|
$this->productAttributeEntityClass = $productAttributeEntityClass; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getTaxonsByAttributeViaProduct(AttributeInterface $attribute): array |
46
|
|
|
{ |
47
|
|
|
return $this->baseTaxonRepository |
|
|
|
|
48
|
|
|
->createQueryBuilder('t') |
|
|
|
|
49
|
|
|
->distinct(true) |
50
|
|
|
->select('t') |
51
|
|
|
->leftJoin($this->productTaxonEntityClass, 'pt', Join::WITH, 'pt.taxon = t.id') |
52
|
|
|
->where( |
53
|
|
|
'pt.product IN(' . |
54
|
|
|
$this |
55
|
|
|
->productRepository->createQueryBuilder('p') |
|
|
|
|
56
|
|
|
->leftJoin($this->productAttributeEntityClass, 'pav', Join::WITH, 'pav.subject = p.id') |
57
|
|
|
->where('pav.attribute = :attribute') |
58
|
|
|
->getQuery() |
59
|
|
|
->getDQL() |
60
|
|
|
. ')' |
61
|
|
|
) |
62
|
|
|
->setParameter(':attribute', $attribute) |
63
|
|
|
->getQuery() |
64
|
|
|
->getResult() |
65
|
|
|
; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|