From a8660e75985728843a5bfcf249c50d9a611c42f6 Mon Sep 17 00:00:00 2001 From: Mikel Date: Thu, 1 Dec 2022 16:23:33 +0100 Subject: [PATCH 1/4] Add missing method into EntityInterface --- Domain/Model/EntityInterface.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Domain/Model/EntityInterface.php b/Domain/Model/EntityInterface.php index 24ffe66..2e91086 100755 --- a/Domain/Model/EntityInterface.php +++ b/Domain/Model/EntityInterface.php @@ -27,6 +27,8 @@ public function hasBeenDeleted(): bool; public function __toString(): string; + public function isInitialized(): bool; + public function initChangelog(): void; /** From 1bc73e3066df12a5d348856e9b9752e1dbe7a2c6 Mon Sep 17 00:00:00 2001 From: Mikel Date: Wed, 28 Dec 2022 10:06:21 +0100 Subject: [PATCH 2/4] add missing dependency --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index fb69620..6be8f68 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,8 @@ "symfony/finder": "^5.1", "symfony/http-foundation": "^5.1", "symfony/serializer": "^5.1", - "symfony/mailer": "^5.4" + "symfony/mailer": "^5.4", + "symfony/mime": "^5.4" }, "require-dev": { "phpstan/phpstan": "^1.0" From 9aee110cc225184219590cee7051e22f6e6b2e13 Mon Sep 17 00:00:00 2001 From: Mikel Date: Thu, 29 Dec 2022 10:25:43 +0100 Subject: [PATCH 3/4] replace references EntityManager by EntityManagerInterface --- Application/Service/DataGateway.php | 12 +++--------- Application/Service/EntityTools.php | 4 ++-- Application/Service/QueryBuilderFactory.php | 6 +++--- .../Application/DoctrineForeignKeyTransformer.php | 6 +++--- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/Application/Service/DataGateway.php b/Application/Service/DataGateway.php index 5fa6672..07b585e 100755 --- a/Application/Service/DataGateway.php +++ b/Application/Service/DataGateway.php @@ -2,7 +2,7 @@ namespace Ivoz\Core\Application\Service; -use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityManagerInterface; use Ivoz\Core\Application\DataTransferObjectInterface; use Ivoz\Core\Application\Event\CommandWasExecuted; use Ivoz\Core\Application\RequestId; @@ -18,7 +18,7 @@ class DataGateway { /** - * @var EntityManager + * @var EntityManagerInterface */ private $em; @@ -54,15 +54,9 @@ class DataGateway /** * DataGateway constructor. - * @param EntityManager $entityManager - * @param QueryBuilderFactory $queryBuilderFactory - * @param DoctrineEntityPersister $entityPersister - * @param DtoAssembler $dtoAssembler - * @param DomainEventPublisher $eventPublisher - * @param RequestId $requestId */ public function __construct( - EntityManager $entityManager, + EntityManagerInterface $entityManager, QueryBuilderFactory $queryBuilderFactory, DoctrineEntityPersister $entityPersister, DtoAssembler $dtoAssembler, diff --git a/Application/Service/EntityTools.php b/Application/Service/EntityTools.php index 4cfb8db..45b942a 100644 --- a/Application/Service/EntityTools.php +++ b/Application/Service/EntityTools.php @@ -3,7 +3,7 @@ namespace Ivoz\Core\Application\Service; use Doctrine\DBAL\LockMode; -use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\OptimisticLockException; use Doctrine\ORM\PessimisticLockException; use Ivoz\Core\Application\DataTransferObjectInterface; @@ -25,7 +25,7 @@ class EntityTools private $entityUpdater; public function __construct( - EntityManager $entityManager, + EntityManagerInterface $entityManager, EntityPersisterInterface $entityPersister, DtoAssembler $dtoAssembler, CreateEntityFromDto $createEntityFromDto, diff --git a/Application/Service/QueryBuilderFactory.php b/Application/Service/QueryBuilderFactory.php index b3ab753..b7cd064 100644 --- a/Application/Service/QueryBuilderFactory.php +++ b/Application/Service/QueryBuilderFactory.php @@ -2,17 +2,17 @@ namespace Ivoz\Core\Application\Service; -use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\QueryBuilder; class QueryBuilderFactory { /** - * @var EntityManager + * @var EntityManagerInterface */ private $em; - public function __construct(EntityManager $entityManager) + public function __construct(EntityManagerInterface $entityManager) { $this->em = $entityManager; } diff --git a/Infrastructure/Application/DoctrineForeignKeyTransformer.php b/Infrastructure/Application/DoctrineForeignKeyTransformer.php index 701965b..06d9d90 100755 --- a/Infrastructure/Application/DoctrineForeignKeyTransformer.php +++ b/Infrastructure/Application/DoctrineForeignKeyTransformer.php @@ -3,7 +3,7 @@ namespace Ivoz\Core\Infrastructure\Application; use Doctrine\Common\Collections\ArrayCollection; -use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityManagerInterface; use Ivoz\Core\Application\DataTransferObjectInterface; use Ivoz\Core\Application\ForeignKeyTransformerInterface; use Ivoz\Core\Application\Helper\EntityClassHelper; @@ -12,11 +12,11 @@ class DoctrineForeignKeyTransformer implements ForeignKeyTransformerInterface { /** - * @var EntityManager + * @var EntityManagerInterface */ private $em; - public function __construct(EntityManager $em) + public function __construct(EntityManagerInterface $em) { $this->em = $em; } From 5b11c7bffd9d410953f62ce93adfea05188e4e4e Mon Sep 17 00:00:00 2001 From: Mikel Date: Thu, 29 Dec 2022 10:26:42 +0100 Subject: [PATCH 4/4] make EntityManager extend doctrine EntityManagerDecorator --- .../Doctrine/ORM/EntityManager.php | 67 ++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/Infrastructure/Persistence/Doctrine/ORM/EntityManager.php b/Infrastructure/Persistence/Doctrine/ORM/EntityManager.php index d8a9038..1749c0a 100755 --- a/Infrastructure/Persistence/Doctrine/ORM/EntityManager.php +++ b/Infrastructure/Persistence/Doctrine/ORM/EntityManager.php @@ -6,13 +6,18 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\DriverManager; use Doctrine\ORM\Configuration; +use Doctrine\ORM\Decorator\EntityManagerDecorator; use Doctrine\ORM\EntityManager as DoctrineEntityManager; +use Doctrine\ORM\NativeQuery; use Doctrine\ORM\ORMException; +use Doctrine\ORM\Query\ResultSetMapping; +use Doctrine\ORM\QueryBuilder; +use Doctrine\ORM\Query as DqlQuery; use Ivoz\Core\Infrastructure\Persistence\Doctrine\Hydration\ObjectHydrator; use Ivoz\Core\Infrastructure\Persistence\Doctrine\Hydration\SimpleObjectHydrator; use Ivoz\Core\Infrastructure\Persistence\Doctrine\Hydration\DtoHydrator; -class EntityManager extends DoctrineEntityManager implements ToggleableBufferedQueryInterface +class EntityManager extends EntityManagerDecorator implements ToggleableBufferedQueryInterface { public function enableBufferedQuery() { @@ -67,7 +72,30 @@ public static function create($conn, Configuration $config, EventManager $eventM throw new \InvalidArgumentException("Invalid argument: " . $conn); } - return new self($conn, $config, $conn->getEventManager()); + $emRef = new \ReflectionClass( + DoctrineEntityManager::class + ); + /** @var DoctrineEntityManager $em */ + $em = $emRef->newInstanceWithoutConstructor(); + $eventManager = $conn->getEventManager(); + (function () use ($conn, $config, $eventManager) { + $this->__construct($conn, $config, $eventManager); + })->call($em); + + $instance = new self($em); + (function () use ($instance) { + $this->em = $instance; + })->call($instance->getUnitOfWork()); + + return $instance; + } + + /** + * {@inheritDoc} + */ + public function getHydrator($hydrationMode) + { + return $this->newHydrator($hydrationMode); } /** @@ -89,4 +117,39 @@ public function newHydrator($hydrationMode) return parent::newHydrator(...func_get_args()); } } + + /** + * {@inheritDoc} + */ + public function createQuery($dql = '') + { + $query = new DqlQuery($this); + + if (! empty($dql)) { + $query->setDQL($dql); + } + + return $query; + } + + /** + * {@inheritDoc} + */ + public function createNativeQuery($sql, ResultSetMapping $rsm) + { + $query = new NativeQuery($this); + + $query->setSQL($sql); + $query->setResultSetMapping($rsm); + + return $query; + } + + /** + * {@inheritDoc} + */ + public function createQueryBuilder() + { + return new QueryBuilder($this); + } }