|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher Storage Bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please see the |
|
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2016 Sourcefabric z.ú |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace SWP\Bundle\StorageBundle\Doctrine\ORM; |
|
16
|
|
|
|
|
17
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
18
|
|
|
use Knp\Component\Pager\Paginator; |
|
19
|
|
|
use Knp\Component\Pager\Pagination\PaginationInterface; |
|
20
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
|
21
|
|
|
use SWP\Component\Common\Pagination\PaginationData; |
|
22
|
|
|
use SWP\Component\Storage\Model\PersistableInterface; |
|
23
|
|
|
|
|
24
|
|
|
trait EntityRepositoryTrait |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function persist(PersistableInterface $object) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->_em->persist($object); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function add(PersistableInterface $object) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->_em->persist($object); |
|
40
|
|
|
$this->_em->flush(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function flush() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->_em->flush(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
View Code Duplication |
public function remove(PersistableInterface $object) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
if (null !== $this->find($object->getId())) { |
|
|
|
|
|
|
57
|
|
|
$this->_em->remove($object); |
|
58
|
|
|
$this->_em->flush(); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getPaginatedByCriteria(Criteria $criteria, array $sorting = [], PaginationData $paginationData = null) |
|
66
|
|
|
{ |
|
67
|
|
|
$queryBuilder = $this->getQueryByCriteria($criteria, $sorting, 's'); |
|
68
|
|
|
|
|
69
|
|
|
if (null === $paginationData) { |
|
70
|
|
|
$paginationData = new PaginationData(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this->getPaginator($queryBuilder, $paginationData); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getQueryByCriteria(Criteria $criteria, array $sorting, string $alias): QueryBuilder |
|
80
|
|
|
{ |
|
81
|
|
|
$queryBuilder = $this->createQueryBuilder($alias); |
|
|
|
|
|
|
82
|
|
|
$this->applyCriteria($queryBuilder, $criteria, $alias); |
|
83
|
|
|
$this->applySorting($queryBuilder, $sorting, $alias); |
|
84
|
|
|
$this->applyLimiting($queryBuilder, $criteria); |
|
85
|
|
|
|
|
86
|
|
|
return $queryBuilder; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param QueryBuilder $queryBuilder |
|
91
|
|
|
* @param Criteria $criteria |
|
92
|
|
|
*/ |
|
93
|
|
|
public function applyLimiting(QueryBuilder $queryBuilder, Criteria $criteria) |
|
94
|
|
|
{ |
|
95
|
|
|
$queryBuilder->setFirstResult($criteria->get('firstResult', 0)); |
|
96
|
|
|
if ($criteria->has('maxResults')) { |
|
97
|
|
|
$queryBuilder->setMaxResults($criteria->get('maxResults')); |
|
98
|
|
|
} else { |
|
99
|
|
|
$queryBuilder->setMaxResults(self::MAX_RESULTS); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param $queryBuilder |
|
105
|
|
|
* @param PaginationData $paginationData |
|
106
|
|
|
* |
|
107
|
|
|
* @return PaginationInterface |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function getPaginator($queryBuilder, PaginationData $paginationData) |
|
110
|
|
|
{ |
|
111
|
|
|
$paginator = new Paginator(); |
|
112
|
|
|
|
|
113
|
|
|
return $paginator->paginate($queryBuilder, $paginationData->getPageNumber(), $paginationData->getLimit()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param QueryBuilder $queryBuilder |
|
118
|
|
|
* @param Criteria $criteria |
|
119
|
|
|
* @param string $alias |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function applyCriteria(QueryBuilder $queryBuilder, Criteria $criteria, string $alias) |
|
122
|
|
|
{ |
|
123
|
|
|
$properties = array_merge($this->getClassMetadata()->getFieldNames(), $this->getClassMetadata()->getAssociationNames()); |
|
|
|
|
|
|
124
|
|
|
foreach ($criteria->all() as $property => $value) { |
|
125
|
|
|
if (!in_array($property, $properties)) { |
|
126
|
|
|
continue; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$name = $this->getPropertyName($property, $alias); |
|
130
|
|
|
if (null === $value) { |
|
131
|
|
|
$queryBuilder->andWhere($queryBuilder->expr()->isNull($name)); |
|
132
|
|
|
} elseif (is_array($value)) { |
|
133
|
|
|
$queryBuilder->andWhere($queryBuilder->expr()->in($name, $value)); |
|
134
|
|
|
} elseif ('' !== $value) { |
|
135
|
|
|
$parameter = str_replace('.', '_', $property); |
|
136
|
|
|
$queryBuilder |
|
137
|
|
|
->andWhere($queryBuilder->expr()->eq($name, ':'.$parameter)) |
|
138
|
|
|
->setParameter($parameter, $value) |
|
139
|
|
|
; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param QueryBuilder $queryBuilder |
|
146
|
|
|
* @param array $sorting |
|
147
|
|
|
* @param string $alias |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function applySorting(QueryBuilder $queryBuilder, array $sorting, string $alias) |
|
150
|
|
|
{ |
|
151
|
|
|
foreach ($sorting as $property => $order) { |
|
152
|
|
|
if (!empty($order)) { |
|
153
|
|
|
$queryBuilder->addOrderBy($this->getPropertyName($property, $alias), $order); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $name |
|
160
|
|
|
* @param string $alias |
|
161
|
|
|
* |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function getPropertyName(string $name, string $alias) |
|
165
|
|
|
{ |
|
166
|
|
|
if (false === strpos($name, '.')) { |
|
167
|
|
|
return $alias.'.'.$name; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $name; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: