1 | <?php |
||
33 | class RelationalRepository extends EntityRepository implements Repository, SpecificationAwareInterface |
||
34 | { |
||
35 | use SpecificationRepositoryTrait; |
||
36 | use RepositoryTrait; |
||
37 | use EventsTrait; |
||
38 | use PaginatorTrait; |
||
39 | |||
40 | /** |
||
41 | * Class name. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $className; |
||
46 | |||
47 | /** |
||
48 | * Class alias. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $classAlias; |
||
53 | |||
54 | /** |
||
55 | * Get class alias. |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | protected function getClassAlias(): string |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function getClassName(): string |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | protected function getManager(): EntityManager |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | * |
||
91 | * @param array $criteria |
||
92 | * @param array|null $orderBy |
||
93 | * @param int $itemsPerPage |
||
94 | * |
||
95 | * @return \Zend\Paginator\Paginator |
||
96 | */ |
||
97 | public function findPaginatedBy($criteria, array $orderBy = [], int $itemsPerPage = 10): Paginator |
||
103 | |||
104 | /** |
||
105 | * Get query builder from criteria array. |
||
106 | * |
||
107 | * @param array $criteria |
||
108 | * @param array $orderBy |
||
109 | * |
||
110 | * @return QueryBuilder |
||
111 | */ |
||
112 | protected function getQueryBuilderFromCriteria(array $criteria, array $orderBy = []): QueryBuilder |
||
113 | { |
||
114 | $entityAlias = $this->getClassAlias(); |
||
115 | $queryBuilder = $this->createQueryBuilder($entityAlias); |
||
116 | $entityAlias = count($queryBuilder->getRootAliases()) ? $queryBuilder->getRootAliases()[0] : $entityAlias; |
||
117 | |||
118 | foreach ($criteria as $field => $value) { |
||
119 | $this->addQueryCriteria($queryBuilder, $field, $value, $entityAlias); |
||
120 | } |
||
121 | |||
122 | if (is_array($orderBy)) { |
||
123 | foreach ($orderBy as $field => $order) { |
||
124 | $queryBuilder->addOrderBy($entityAlias . '.' . $field, $order); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return $queryBuilder; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Add query builder criteria. |
||
133 | * |
||
134 | * @param QueryBuilder $queryBuilder |
||
135 | * @param string $field |
||
136 | * @param mixed $value |
||
137 | * @param string $entityAlias |
||
138 | */ |
||
139 | protected function addQueryCriteria(QueryBuilder $queryBuilder, string $field, $value, string $entityAlias) |
||
140 | { |
||
141 | if ($value === null) { |
||
142 | $queryBuilder->andWhere(sprintf('%s.%s IS NULL', $entityAlias, $field)); |
||
143 | } else { |
||
144 | $placeholder = sprintf('%s_%s', $field, substr(sha1($field), 0, 5)); |
||
145 | |||
146 | if (is_array($value)) { |
||
147 | $queryBuilder->andWhere(sprintf('%s.%s IN (:%s)', $entityAlias, $field, $placeholder)); |
||
148 | } else { |
||
149 | $queryBuilder->andWhere(sprintf('%s.%s = :%s', $entityAlias, $field, $placeholder)); |
||
150 | } |
||
151 | |||
152 | $queryBuilder->setParameter($placeholder, $value); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Paginate query. |
||
158 | * |
||
159 | * @param Query $query |
||
160 | * @param int $itemsPerPage |
||
161 | * |
||
162 | * @return Paginator |
||
163 | */ |
||
164 | protected function paginate(Query $query, int $itemsPerPage = 10): Paginator |
||
165 | { |
||
166 | return $this->getPaginator(new RelationalPaginatorAdapter(new RelationalPaginator($query)), $itemsPerPage); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | * |
||
172 | * @param array|QueryBuilder $criteria |
||
173 | * |
||
174 | * @return int |
||
175 | */ |
||
176 | public function countBy($criteria): int |
||
180 | } |
||
181 |