|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Kunstmaan\AdminBundle\Event\DeepCloneAndSaveEvent; |
|
7
|
|
|
use Kunstmaan\AdminBundle\Event\Events; |
|
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
9
|
|
|
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This helper will help you to clone Entities |
|
13
|
|
|
*/ |
|
14
|
|
|
class CloneHelper |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var EntityManager |
|
18
|
|
|
*/ |
|
19
|
|
|
private $em; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var EventDispatcherInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $eventDispatcher; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param EntityManager $em The EntityManager |
|
28
|
|
|
* @param EventDispatcherInterface $eventDispatcher The EventDispatchInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(EntityManager $em, EventDispatcherInterface $eventDispatcher) |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
$this->em = $em; |
|
33
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param mixed $entity |
|
38
|
|
|
* |
|
39
|
|
|
* @return mixed |
|
|
|
|
|
|
40
|
|
|
*/ |
|
41
|
|
|
public function deepCloneAndSave($entity) |
|
42
|
|
|
{ |
|
43
|
|
|
$clonedEntity = clone $entity; |
|
44
|
|
|
$this->dispatch(new DeepCloneAndSaveEvent($entity, $clonedEntity), Events::DEEP_CLONE_AND_SAVE); |
|
45
|
|
|
|
|
46
|
|
|
$this->em->persist($clonedEntity); |
|
47
|
|
|
$this->em->flush(); |
|
48
|
|
|
|
|
49
|
|
|
$this->dispatch(new DeepCloneAndSaveEvent($entity, $clonedEntity), Events::POST_DEEP_CLONE_AND_SAVE); |
|
50
|
|
|
|
|
51
|
|
|
return $clonedEntity; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param object $event |
|
56
|
|
|
* @param string $eventName |
|
57
|
|
|
* |
|
58
|
|
|
* @return object |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
private function dispatch($event, string $eventName) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
if (class_exists(LegacyEventDispatcherProxy::class)) { |
|
63
|
|
|
$eventDispatcher = LegacyEventDispatcherProxy::decorate($this->eventDispatcher); |
|
64
|
|
|
|
|
65
|
|
|
return $eventDispatcher->dispatch($event, $eventName); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->eventDispatcher->dispatch($eventName, $event); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
The
EntityManagermight 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
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring 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.