Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

EntityManager.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Lc\SovBundle\Doctrine;
  3. use Doctrine\ORM\Decorator\EntityManagerDecorator;
  4. use Doctrine\ORM\EntityManager as DoctrineEntityManager;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Knp\Component\Pager\PaginatorInterface;
  7. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  8. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. /**
  11. * class EntityManager.
  12. *
  13. * @author La clic !!!!
  14. */
  15. class EntityManager extends EntityManagerDecorator
  16. {
  17. protected EventDispatcherInterface $eventDispatcher;
  18. protected DoctrineEntityManager $entityManager;
  19. public function __construct(EntityManagerInterface $wrapped, EventDispatcherInterface $eventDispatcher)
  20. {
  21. $this->eventDispatcher = $eventDispatcher;
  22. parent::__construct($wrapped);
  23. }
  24. public function createQueryBuilder()
  25. {
  26. return new QueryBuilder($this);
  27. }
  28. public function getRepository($className)
  29. {
  30. return $this->wrapped->getRepository($this->getEntityName($className));
  31. }
  32. public function new($className)
  33. {
  34. $entityName = $this->getEntityName($className);
  35. return new $entityName;
  36. }
  37. public function create(EntityInterface $entity, bool $dispatchEvent = true): self
  38. {
  39. if($dispatchEvent) {
  40. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_CREATE_EVENT);
  41. }
  42. $this->persist($entity);
  43. if($dispatchEvent) {
  44. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_CREATE_EVENT);
  45. }
  46. return $this;
  47. }
  48. public function update(EntityInterface $entity, bool $dispatchEvent = true): self
  49. {
  50. if($dispatchEvent){
  51. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_UPDATE_EVENT);
  52. }
  53. $this->persist($entity);
  54. if($dispatchEvent) {
  55. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_UPDATE_EVENT);
  56. }
  57. return $this;
  58. }
  59. public function delete(EntityInterface $entity, bool $dispatchEvent = true): self
  60. {
  61. if($dispatchEvent) {
  62. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_DELETE_EVENT);
  63. }
  64. $this->remove($entity);
  65. if($dispatchEvent) {
  66. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_DELETE_EVENT);
  67. }
  68. return $this;
  69. }
  70. public function flush($entity = null): self
  71. {
  72. $this->wrapped->flush($entity);
  73. return $this;
  74. }
  75. public function clear($objectName = null): self
  76. {
  77. $this->wrapped->clear($objectName);
  78. return $this;
  79. }
  80. public function refresh($object): self
  81. {
  82. $this->wrapped->refresh($object);
  83. return $this;
  84. }
  85. public function persist($entity)
  86. {
  87. $this->wrapped->persist($entity);
  88. }
  89. public function getClassMetadata($className)
  90. {
  91. return $this->wrapped->getClassMetadata($className);
  92. }
  93. public function getEntityName($className)
  94. {
  95. if (substr($className, -9) === 'Interface') {
  96. return $this->wrapped->getClassMetadata($className)->getName();
  97. } else {
  98. return $className;
  99. }
  100. }
  101. }