EntityRepository::getEntityManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 *
8
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
9
 */
10
11
namespace Kdyby\Doctrine;
12
13
use Doctrine;
14
use Doctrine\ORM\AbstractQuery;
15
use Doctrine\ORM\NoResultException;
16
use Doctrine\ORM\NonUniqueResultException;
17
use Kdyby;
18
use Kdyby\Persistence;
19
use Nette;
20
21
22
23
/**
24
 * This class is an extension to EntityRepository and should help you with prototyping.
25
 * The first and only rule with EntityRepository is not to ever inherit them, ever.
26
 *
27
 * The only valid reason to inherit EntityRepository is to add more common methods to all EntityRepositories in application,
28
 * when you're creating your own framework (but do we really need to go any deeper than this?).
29
 *
30
 * @author Filip Procházka <[email protected]>
31
 */
32
class EntityRepository extends Doctrine\ORM\EntityRepository implements Persistence\QueryExecutor, Persistence\Queryable //, Persistence\ObjectFactory
33
{
34
	
35
	public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
36
	{
37
		if ($this->criteriaRequiresDql($criteria) === FALSE && $this->criteriaRequiresDql((array) $orderBy) === FALSE) {
38
			return parent::findBy($criteria, $orderBy, $limit, $offset);
39
		}
40
41
		$qb = $this->createQueryBuilder('e')
42
			->whereCriteria($criteria)
43
			->autoJoinOrderBy((array) $orderBy);
44
45
		return $qb->getQuery()
46
			->setMaxResults($limit)
47
			->setFirstResult($offset)
48
			->getResult();
49
	}
50
51
52
53
	public function findOneBy(array $criteria, array $orderBy = null)
54
	{
55
		if ($this->criteriaRequiresDql($criteria) === FALSE && $this->criteriaRequiresDql((array) $orderBy) === FALSE) {
56
			return parent::findOneBy($criteria, $orderBy);
57
		}
58
59
		$qb = $this->createQueryBuilder('e')
60
			->whereCriteria($criteria)
61
			->autoJoinOrderBy((array) $orderBy);
62
63
		try {
64
			return $qb->setMaxResults(1)
65
				->getQuery()->getSingleResult();
66
67
		} catch (NoResultException $e) {
68
			return NULL;
69
		}
70
	}
71
72
73
74
	/**
75
	 * @param array $criteria
76
	 * @return int
77
	 */
78
	public function countBy(array $criteria = [])
79
	{
80
		return (int) $this->createQueryBuilder('e')
81
			->whereCriteria($criteria)
82
			->select('COUNT(e)')
83
			->getQuery()->getSingleScalarResult();
84
	}
85
86
87
88
	/**
89
	 * @param array $criteria
90
	 * @return bool
91
	 */
92
	private function criteriaRequiresDql(array $criteria)
93
	{
94
		foreach ($criteria as $key => $val) {
95
			if (preg_match('~[\\?\\s\\.]~', $key)) {
96
				return TRUE;
97
			}
98
		}
99
100
		return FALSE;
101
	}
102
103
104
105
	/**
106
	 * Fetches all records like $key => $value pairs
107
	 *
108
	 * @param array $criteria parameter can be skipped
109
	 * @param string $value mandatory
110
	 * @param array|string $orderBy parameter can be skipped
111
	 * @param string $key optional
112
	 *
113
	 * @throws QueryException
114
	 * @return array
115
	 */
116
	public function findPairs($criteria, $value = NULL, $orderBy = [], $key = NULL)
117
	{
118
		if (!is_array($criteria)) {
119
			$key = $orderBy;
120
			$orderBy = $value;
121
			$value = $criteria;
122
			$criteria = [];
123
		}
124
125
		if (!is_array($orderBy)) {
126
			$key = $orderBy;
127
			$orderBy = [];
128
		}
129
130
		if (empty($key)) {
131
			$key = $this->getClassMetadata()->getSingleIdentifierFieldName();
132
		}
133
134
		/** @var \Kdyby\Doctrine\QueryBuilder $qb */
135
		$qb = $this->createQueryBuilder('e')
136
			->whereCriteria($criteria)
137
			->select(["e.$value", "e.$key"])
138
			->resetDQLPart('from')->from($this->getEntityName(), 'e', 'e.' . $key);
139
		$query = $qb->autoJoinOrderBy($orderBy)->getQuery();
140
141
		try {
142
			return array_map(function ($row) {
143
				return reset($row);
144
			}, $query->getResult(AbstractQuery::HYDRATE_ARRAY));
145
146
		} catch (\Exception $e) {
147
			throw $this->handleException($e, $query);
148
		}
149
	}
150
151
152
153
	/**
154
	 * Fetches all records and returns an associative array indexed by key
155
	 *
156
	 * @param array $criteria
157
	 * @param string $key
158
	 *
159
	 * @throws \Exception|QueryException
160
	 * @return array
161
	 */
162
	public function findAssoc($criteria, $key = NULL)
163
	{
164
		if (!is_array($criteria)) {
165
			$key = $criteria;
166
			$criteria = [];
167
		}
168
169
		$query = $this->createQueryBuilder('e')
170
			->whereCriteria($criteria)
171
			->resetDQLPart('from')->from($this->getEntityName(), 'e', 'e.' . $key)
172
			->getQuery();
173
174
		try {
175
			return $query->getResult();
176
177
		} catch (\Exception $e) {
178
			throw $this->handleException($e, $query);
179
		}
180
	}
181
182
183
184
	/**
185
	 * @param string $sql
186
	 * @param Doctrine\ORM\Query\ResultSetMapping $rsm
187
	 * @return Doctrine\ORM\NativeQuery
188
	 */
189
	public function createNativeQuery($sql, Doctrine\ORM\Query\ResultSetMapping $rsm)
190
	{
191
		return $this->getEntityManager()->createNativeQuery($sql, $rsm);
192
	}
193
194
195
196
	/**
197
	 * @param string $alias
198
	 * @param string $indexBy The index for the from.
199
	 * @return \Kdyby\Doctrine\QueryBuilder
200
	 */
201
	public function createQueryBuilder($alias = NULL, $indexBy = NULL)
202
	{
203
		$qb = $this->getEntityManager()->createQueryBuilder();
204
205
		if ($alias !== NULL) {
206
			$qb->select($alias)->from($this->getEntityName(), $alias, $indexBy);
207
		}
208
209
		return $qb;
210
	}
211
212
213
214
	/**
215
	 * @param string $dql
216
	 *
217
	 * @return \Doctrine\ORM\Query
218
	 */
219
	public function createQuery($dql = NULL)
220
	{
221
		$dql = implode(' ', func_get_args());
222
223
		return $this->getEntityManager()->createQuery($dql);
224
	}
225
226
227
228
	/**
229
	 * @param \Kdyby\Persistence\Query|\Kdyby\Doctrine\QueryObject $queryObject
230
	 * @param int $hydrationMode
231
	 * @throws QueryException
232
	 * @return array|\Kdyby\Doctrine\ResultSet
233
	 */
234
	public function fetch(Persistence\Query $queryObject, $hydrationMode = AbstractQuery::HYDRATE_OBJECT)
235
	{
236
		try {
237
			return $queryObject->fetch($this, $hydrationMode);
238
239
		} catch (\Exception $e) {
240
			throw $this->handleQueryException($e, $queryObject);
241
		}
242
	}
243
244
245
246
	/**
247
	 * @param \Kdyby\Persistence\Query|\Kdyby\Doctrine\QueryObject $queryObject
248
	 *
249
	 * @throws InvalidStateException
250
	 * @throws QueryException
251
	 * @return object|null
252
	 */
253
	public function fetchOne(Persistence\Query $queryObject)
254
	{
255
		try {
256
			return $queryObject->fetchOne($this);
257
258
		} catch (NoResultException $e) {
259
			return NULL;
260
261
		} catch (NonUniqueResultException $e) { // this should never happen!
262
			throw new InvalidStateException("You have to setup your query calling ->setMaxResult(1).", 0, $e);
263
264
		} catch (\Exception $e) {
265
			throw $this->handleQueryException($e, $queryObject);
266
		}
267
	}
268
269
270
271
	/**
272
	 * @param integer|array $id
273
	 * @return \Doctrine\ORM\Proxy\Proxy|null
274
	 */
275
	public function getReference($id)
276
	{
277
		/** @var \Doctrine\ORM\Proxy\Proxy|null $reference */
278
		$reference = $this->getEntityManager()->getReference($this->_entityName, $id);
279
		return $reference;
280
	}
281
282
283
284
	/**
285
	 * @param \Exception $e
286
	 * @param \Kdyby\Persistence\Query $queryObject
287
	 * @return \Kdyby\Doctrine\QueryException
288
	 */
289
	private function handleQueryException(\Exception $e, Persistence\Query $queryObject)
290
	{
291
		$lastQuery = $queryObject instanceof QueryObject ? $queryObject->getLastQuery() : NULL;
292
293
		return new QueryException($e, $lastQuery, '[' . get_class($queryObject) . '] ' . $e->getMessage());
294
	}
295
296
297
298
	/**
299
	 * @param \Exception $e
300
	 * @param \Doctrine\ORM\Query $query
301
	 * @param string $message
302
	 * @return \Exception|\Kdyby\Doctrine\QueryException
303
	 */
304
	private function handleException(\Exception $e, Doctrine\ORM\Query $query = NULL, $message = NULL)
305
	{
306
		if ($e instanceof Doctrine\ORM\Query\QueryException) {
307
			return new QueryException($e, $query, $message);
308
		}
309
310
		return $e;
311
	}
312
313
314
315
	/**
316
	 * @return \Kdyby\Doctrine\Mapping\ClassMetadata
317
	 */
318
	public function getClassMetadata()
319
	{
320
		/** @var \Kdyby\Doctrine\Mapping\ClassMetadata $classMetadata */
321
		$classMetadata = parent::getClassMetadata();
322
		return $classMetadata;
323
	}
324
325
326
327
	/**
328
	 * @return \Kdyby\Doctrine\EntityManager
329
	 */
330
	public function getEntityManager()
331
	{
332
		/** @var \Kdyby\Doctrine\EntityManager $entityManager */
333
		$entityManager = parent::getEntityManager();
334
		return $entityManager;
335
	}
336
337
338
339
	/**
340
	 * @param string $relation
341
	 * @return \Kdyby\Doctrine\EntityRepository
342
	 */
343
	public function related($relation)
344
	{
345
		$meta = $this->getClassMetadata();
346
		$targetClass = $meta->getAssociationTargetClass($relation);
347
348
		/** @var \Kdyby\Doctrine\EntityRepository $entityRepository */
349
		$entityRepository = $this->getEntityManager()->getRepository($targetClass);
350
		return $entityRepository;
351
	}
352
353
}
354