1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Caridea |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Caridea\Dao; |
22
|
|
|
|
23
|
|
|
use Doctrine\ORM\EntityManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* An abstract Data Access Object for the Doctrine ORM library. |
27
|
|
|
* |
28
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
29
|
|
|
* @license Apache-2.0 |
30
|
|
|
*/ |
31
|
|
|
abstract class Doctrine2 extends Dao |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var \Doctrine\ORM\EntityManager The entity manager |
35
|
|
|
*/ |
36
|
|
|
protected $manager; |
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $entityName; |
41
|
|
|
/** |
42
|
|
|
* @var \Doctrine\ORM\EntityRepository The entity repository |
43
|
|
|
*/ |
44
|
|
|
protected $repository; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Creates a new MongoDB DAO. |
48
|
|
|
* |
49
|
|
|
* @param \Doctrine\ORM\EntityManager $manager The entity manager |
50
|
|
|
* @param string $entityName The entity name |
51
|
|
|
* @param \Psr\Log\LoggerInterface $logger A logger |
52
|
|
|
*/ |
53
|
4 |
|
public function __construct(EntityManager $manager, string $entityName, \Psr\Log\LoggerInterface $logger = null) |
|
|
|
|
54
|
|
|
{ |
55
|
4 |
|
parent::__construct($logger); |
56
|
4 |
|
$this->manager = $manager; |
57
|
4 |
|
$this->entityName = $entityName; |
58
|
4 |
|
$this->repository = $manager->getRepository($entityName); |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Executes something in the context of the entityManager. |
63
|
|
|
* |
64
|
|
|
* Exceptions are caught and translated. |
65
|
|
|
* |
66
|
|
|
* @param Closure $cb The closure to execute, takes the entityManager |
67
|
|
|
* @return mixed whatever the function returns, this method also returns |
68
|
|
|
* @throws \Caridea\Dao\Exception If a database problem occurs |
69
|
|
|
* @see \Caridea\Dao\Exception\Translator\Doctrine |
70
|
|
|
*/ |
71
|
2 |
|
protected function doExecute(\Closure $cb) |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
2 |
|
return $cb($this->manager); |
75
|
1 |
|
} catch (\Exception $e) { |
76
|
1 |
|
throw Exception\Translator\Doctrine::translate($e); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Executes something in the context of the entityRepository. |
82
|
|
|
* |
83
|
|
|
* Exceptions are caught and translated. |
84
|
|
|
* |
85
|
|
|
* @param Closure $cb The closure to execute, takes the entityRepository |
86
|
|
|
* @return mixed whatever the function returns, this method also returns |
87
|
|
|
* @throws \Caridea\Dao\Exception If a database problem occurs |
88
|
|
|
* @see \Caridea\Dao\Exception\Translator\Doctrine |
89
|
|
|
*/ |
90
|
2 |
|
protected function doExecuteInRepository(\Closure $cb) |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
2 |
|
return $cb($this->repository); |
94
|
1 |
|
} catch (\Exception $e) { |
95
|
1 |
|
throw Exception\Translator\Doctrine::translate($e); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.