|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainAltapayBundle\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
7
|
|
|
use Doctrine\ORM\EntityRepository as DoctrineEntityRepository; |
|
8
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
9
|
|
|
use Knp\Component\Pager\Pagination\PaginationInterface; |
|
10
|
|
|
use Knp\Component\Pager\PaginatorInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* This entity repository is implemented using the principles described here: |
|
14
|
|
|
* https://www.tomasvotruba.cz/blog/2017/10/16/how-to-use-repository-with-doctrine-as-service-in-symfony/ |
|
15
|
|
|
* |
|
16
|
|
|
* @todo this class should probably be in a separate library |
|
17
|
|
|
* |
|
18
|
|
|
* @method null|object find($id) |
|
19
|
|
|
* @method array findBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null) |
|
20
|
|
|
* @method null|object findOneBy(array $criteria) |
|
21
|
|
|
* @method array findAll() |
|
22
|
|
|
* @method persist($object) |
|
23
|
|
|
* @method flush() |
|
24
|
|
|
* @method remove($object) |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class EntityRepository |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ObjectManager |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $manager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var DoctrineEntityRepository |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $repository; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var PaginatorInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $paginator; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(ManagerRegistry $managerRegistry, PaginatorInterface $paginator, string $class) { |
|
44
|
|
|
$this->manager = $managerRegistry->getManagerForClass($class); |
|
45
|
|
|
$this->repository = $this->manager->getRepository($class); |
|
46
|
|
|
$this->paginator = $paginator; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $name |
|
51
|
|
|
* @param array $arguments |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __call($name, $arguments) |
|
55
|
|
|
{ |
|
56
|
|
|
if (method_exists($this->repository, $name)) { |
|
57
|
|
|
return call_user_func_array([$this->repository, $name], $arguments); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (method_exists($this->manager, $name)) { |
|
61
|
|
|
return call_user_func_array([$this->manager, $name], $arguments); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Saves the $object |
|
67
|
|
|
* |
|
68
|
|
|
* @param $object |
|
69
|
|
|
*/ |
|
70
|
|
|
public function save($object) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->manager->persist($object); |
|
73
|
|
|
$this->manager->flush(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param int $page |
|
78
|
|
|
* @param int $itemsPerPage |
|
79
|
|
|
* @param array $orderBy |
|
80
|
|
|
* @param QueryBuilder $qb |
|
81
|
|
|
* @return PaginationInterface |
|
82
|
|
|
*/ |
|
83
|
|
|
public function findAllWithPaging($page = 1, $itemsPerPage = 100, array $orderBy = [], QueryBuilder $qb = null) : PaginationInterface |
|
84
|
|
|
{ |
|
85
|
|
|
if(!$qb) { |
|
86
|
|
|
$qb = $this->getQueryBuilder('e'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
foreach ($orderBy as $field => $direction) { |
|
90
|
|
|
$qb->addOrderBy($field, $direction); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$objs = $this->paginator->paginate( |
|
94
|
|
|
$qb, |
|
95
|
|
|
$page, |
|
96
|
|
|
$itemsPerPage |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
return $objs; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $alias |
|
104
|
|
|
* @return QueryBuilder |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getQueryBuilder(string $alias) : QueryBuilder |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->repository->createQueryBuilder($alias); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|