-
Notifications
You must be signed in to change notification settings - Fork 159
/
SectionRepository.php
executable file
·80 lines (69 loc) · 2.66 KB
/
SectionRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* 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
*/
declare(strict_types=1);
namespace BitBag\SyliusCmsPlugin\Repository;
use BitBag\SyliusCmsPlugin\Entity\SectionInterface;
use Doctrine\ORM\QueryBuilder;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
class SectionRepository extends EntityRepository implements SectionRepositoryInterface
{
public function createListQueryBuilder(string $localeCode): QueryBuilder
{
return $this->createQueryBuilder('o')
->addSelect('translation')
->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :localeCode')
->setParameter('localeCode', $localeCode)
;
}
public function findByNamePart(string $phrase, ?string $locale = null): array
{
return $this->createTranslationBasedQueryBuilder($locale)
->andWhere('translation.name LIKE :name')
->setParameter('name', '%' . $phrase . '%')
->getQuery()
->getResult()
;
}
private function createTranslationBasedQueryBuilder(?string $locale = null): QueryBuilder
{
$queryBuilder = $this->createQueryBuilder('o')
->addSelect('translation')
->leftJoin('o.translations', 'translation')
;
if (null !== $locale) {
$queryBuilder
->andWhere('translation.locale = :locale')
->setParameter('locale', $locale)
;
}
return $queryBuilder;
}
public function findOneByCode(string $code, ?string $localeCode): ?SectionInterface
{
return $this->createQueryBuilder('o')
->leftJoin('o.translations', 'translation')
->where('translation.locale = :localeCode')
->andWhere('o.code = :code')
->setParameter('code', $code)
->setParameter('localeCode', $localeCode)
->getQuery()
->getOneOrNullResult()
;
}
public function findByCodesAndLocale(string $codes, string $localeCode): array
{
return $this->createQueryBuilder('o')
->leftJoin('o.translations', 'translation')
->where('translation.locale = :localeCode')
->andWhere('o.code IN(:codes)')
->setParameter('codes', explode(',', $codes))
->setParameter('localeCode', $localeCode)
->getQuery()
->getResult()
;
}
}