1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nord\Lumen\Core\Traits; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
|
8
|
|
|
trait ManagesEntities |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param mixed $entity |
12
|
|
|
*/ |
13
|
|
|
private function saveEntity($entity) |
14
|
|
|
{ |
15
|
|
|
$this->getEntityManager()->persist($entity); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param mixed $entity |
20
|
|
|
*/ |
21
|
|
|
private function saveEntityAndCommit($entity) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$this->saveEntity($entity); |
24
|
|
|
$this->commitEntities(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param mixed $entity |
29
|
|
|
*/ |
30
|
|
|
private function updateEntity($entity) |
31
|
|
|
{ |
32
|
|
|
$this->getEntityManager()->merge($entity); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param mixed $entity |
37
|
|
|
*/ |
38
|
|
|
private function updateEntityAndCommit($entity) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$this->updateEntity($entity); |
41
|
|
|
$this->commitEntities(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param mixed $entity |
46
|
|
|
*/ |
47
|
|
|
private function deleteEntity($entity) |
48
|
|
|
{ |
49
|
|
|
$this->getEntityManager()->remove($entity); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param mixed $entity |
54
|
|
|
*/ |
55
|
|
|
private function deleteEntityAndCommit($entity) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$this->deleteEntity($entity); |
58
|
|
|
$this->commitEntities(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
*/ |
64
|
|
|
private function commitEntities() |
65
|
|
|
{ |
66
|
|
|
$this->getEntityManager()->flush(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param mixed $entity |
71
|
|
|
*/ |
72
|
|
|
private function refreshEntity($entity) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$this->getEntityManager()->refresh($entity); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $name |
79
|
|
|
*/ |
80
|
|
|
private function enableEntityFilter($name) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$this->getEntityManager()->getFilters()->enable($name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $name |
87
|
|
|
*/ |
88
|
|
|
private function disableEntityFilter($name) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
// For some weird reason the filter needs to be enabled first in order to be disabled. |
91
|
|
|
$this->getEntityManager()->getFilters()->enable($name); |
92
|
|
|
$this->getEntityManager()->getFilters()->disable($name); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $entityClassName |
97
|
|
|
* |
98
|
|
|
* @return ObjectRepository |
99
|
|
|
*/ |
100
|
|
|
private function getEntityRepository($entityClassName) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
return $this->getEntityManager()->getRepository($entityClassName); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return EntityManagerInterface |
107
|
|
|
*/ |
108
|
|
|
private function getEntityManager() |
109
|
|
|
{ |
110
|
|
|
return app(EntityManagerInterface::class); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|