Skip to content

Commit

Permalink
make EntityManager extend doctrine EntityManagerDecorator
Browse files Browse the repository at this point in the history
  • Loading branch information
mmadariaga committed Dec 29, 2022
1 parent 9aee110 commit 5b11c7b
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions Infrastructure/Persistence/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}
}

0 comments on commit 5b11c7b

Please sign in to comment.