1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CMSBundle\Entity\Repository; |
4
|
|
|
|
5
|
|
|
use Alpixel\Bundle\CMSBundle\Entity\Node; |
6
|
|
|
use Doctrine\ORM\EntityRepository; |
7
|
|
|
|
8
|
|
|
class NodeRepository extends EntityRepository |
9
|
|
|
{ |
10
|
|
View Code Duplication |
public function findAllWithLocale($locale) |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
return $this |
13
|
|
|
->createQueryBuilder('n') |
14
|
|
|
->andWhere('n.published = true') |
15
|
|
|
->andWhere('n.locale = :locale') |
16
|
|
|
->setParameter('locale', $locale) |
17
|
|
|
->orderBy('n.position', 'ASC') |
18
|
|
|
->getQuery() |
19
|
|
|
->getResult(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function findOnePublishedBySlug($slug) |
23
|
|
|
{ |
24
|
|
|
return $this |
25
|
|
|
->createQueryBuilder('n') |
26
|
|
|
->andWhere('n.published = true') |
27
|
|
|
->andWhere('n.slug = :slug') |
28
|
|
|
->setParameter('slug', $slug) |
29
|
|
|
->getQuery() |
30
|
|
|
->getOneOrNullResult(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function findOnePublishedBySlugAndLocale($slug, $locale) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
return $this |
36
|
|
|
->createQueryBuilder('n') |
37
|
|
|
->andWhere('n.published = true') |
38
|
|
|
->andWhere('n.locale = :locale') |
39
|
|
|
->andWhere('n.slug = :slug') |
40
|
|
|
->setParameter('locale', $locale) |
41
|
|
|
->setParameter('slug', $slug) |
42
|
|
|
->getQuery() |
43
|
|
|
->getOneOrNullResult(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function findOneBySlugAndLocale($slug, $locale) |
47
|
|
|
{ |
48
|
|
|
return $this |
49
|
|
|
->createQueryBuilder('n') |
50
|
|
|
->andWhere('n.locale = :locale') |
51
|
|
|
->andWhere('n.slug = :slug') |
52
|
|
|
->setParameter('locale', $locale) |
53
|
|
|
->setParameter('slug', $slug) |
54
|
|
|
->getQuery() |
55
|
|
|
->getOneOrNullResult(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
public function findTranslation(Node $node, $locale) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$nodeSource = null; |
|
|
|
|
61
|
|
|
|
62
|
|
|
// We are checking if the node is the translation provider or translated |
63
|
|
|
// from an other node |
64
|
|
|
if ($node->getTranslationSource() !== null) { |
65
|
|
|
$nodeSource = $node->getTranslationSource(); |
66
|
|
|
if ($nodeSource->getLocale() == $locale) { |
67
|
|
|
return $nodeSource; |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
$nodeSource = $node; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->createQueryBuilder('n') |
74
|
|
|
->addSelect('n') |
75
|
|
|
->andWhere('n.translationSource = :source') |
76
|
|
|
->andWhere('n.locale = :locale') |
77
|
|
|
->setParameters([ |
78
|
|
|
'source' => $nodeSource, |
79
|
|
|
'locale' => $locale, |
80
|
|
|
]) |
81
|
|
|
->getQuery() |
82
|
|
|
->getOneOrNullResult(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.