1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Entity\Infrasctucture; |
4
|
|
|
|
5
|
|
|
use AppBundle\Form\AbstractFormType; |
6
|
|
|
use Doctrine\ORM\EntityRepository; |
7
|
|
|
use Symfony\Component\Intl\Exception\MethodNotImplementedException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Vehsamrak |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractRepository extends EntityRepository |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @return object|null Entity |
17
|
|
|
*/ |
18
|
|
|
public function findOneByFormData(AbstractFormType $formType) |
19
|
|
|
{ |
20
|
|
|
throw new MethodNotImplementedException(__METHOD__); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return \Doctrine\ORM\EntityManager |
25
|
|
|
*/ |
26
|
2 |
|
public function getEntityManager() |
27
|
|
|
{ |
28
|
2 |
|
return $this->_em; |
29
|
|
|
} |
30
|
|
|
|
31
|
14 |
|
public function flush($entity = null) |
32
|
|
|
{ |
33
|
14 |
|
$this->_em->flush($entity); |
34
|
14 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param object $entity |
38
|
|
|
*/ |
39
|
7 |
|
public function persist($entity) |
40
|
|
|
{ |
41
|
7 |
|
$this->_em->persist($entity); |
42
|
7 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param object $entity |
46
|
|
|
*/ |
47
|
1 |
|
public function remove($entity) |
48
|
|
|
{ |
49
|
1 |
|
$this->_em->remove($entity); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int|string|array $id |
54
|
|
|
* @return object|null Entity |
55
|
|
|
*/ |
56
|
22 |
|
public function findOneById($id) |
57
|
|
|
{ |
58
|
22 |
|
return $this->find($id); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param int $limit |
63
|
|
|
* @param int $offset |
64
|
|
|
* @return object[] Entity array |
65
|
|
|
*/ |
66
|
4 |
|
public function findAllWithLimitAndOffset(int $limit = null, int $offset = null): array |
67
|
|
|
{ |
68
|
|
|
// Doctrine can handle correctly only null limits, but not 0 |
69
|
4 |
|
if (!$limit) { $limit = null; } |
70
|
4 |
|
if (!$offset) { $offset = null; } |
71
|
|
|
|
72
|
4 |
|
return $this->findBy( |
73
|
4 |
|
[], |
74
|
4 |
|
null, |
75
|
|
|
$limit, |
76
|
|
|
$offset |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
4 |
|
public function countAll(): int |
81
|
|
|
{ |
82
|
4 |
|
$ids = $this->getClassMetadata()->getIdentifierFieldNames(); |
83
|
4 |
|
$id = reset($ids); |
84
|
4 |
|
$queryBuilder = $this->createQueryBuilder('entity'); |
85
|
4 |
|
$queryBuilder->select($queryBuilder->expr()->count('entity.' . $id)); |
86
|
|
|
|
87
|
4 |
|
return $queryBuilder->getQuery()->getSingleScalarResult(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|