Skip to content

Commit

Permalink
wrapper for $em - fixed case with entity creation
Browse files Browse the repository at this point in the history
replace entity manager by registry
  • Loading branch information
mdiyakov committed Jun 17, 2018
1 parent 872d362 commit 3178196
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 39 deletions.
15 changes: 8 additions & 7 deletions Command/ClearIndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mdiyakov\DoctrineSolrBundle\Command;

use Doctrine\ORM\EntityManager;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\EntityNotFoundException;
use Mdiyakov\DoctrineSolrBundle\Config\Config;
use Mdiyakov\DoctrineSolrBundle\Exception\EntityNotIndexedException;
Expand Down Expand Up @@ -31,25 +31,25 @@ class ClearIndexCommand extends Command
private $updateQueryBuilder;

/**
* @var EntityManager
* @var Registry
*/
private $em;
private $registry;

/**
* @param Config $config
* @param UpdateQueryBuilder $updateQueryBuilder
* @param EntityManager $em
* @param Registry $registry
*/
public function __construct(
Config $config,
UpdateQueryBuilder $updateQueryBuilder,
EntityManager $em
Registry $registry
)
{
$this->config = $config;
$this->possibleEntityTypes = array_keys($this->getAssocEntitiesClasses());
$this->updateQueryBuilder = $updateQueryBuilder;
$this->em = $em;
$this->registry = $registry;

parent::__construct();
}
Expand Down Expand Up @@ -135,6 +135,7 @@ private function deleteByEntityConfig($entityConfig, $id = null)
$schemaName = $entityConfig['schema'];
$updateQuery = $this->updateQueryBuilder->buildUpdateQueryBySchemaName($schemaName);
$schema = $this->config->getSchemaByName($schemaName);
$em = $this->registry->getManagerForClass($entityConfig['class']);

if (empty($id)) {
$discriminatorField = $schema->getDiscriminatorConfigField();
Expand All @@ -146,7 +147,7 @@ private function deleteByEntityConfig($entityConfig, $id = null)
$message = sprintf('Removing of %s is completed successfully', $entityConfig['class']);
} else {
$entityClass = $entityConfig['class'];
$repository = $this->em->getRepository($entityClass);
$repository = $em->getRepository($entityClass);
$entity = $repository->find($id);

if (!$entity) {
Expand Down
27 changes: 18 additions & 9 deletions Command/IndexEntitiesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mdiyakov\DoctrineSolrBundle\Command;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -17,9 +18,9 @@ class IndexEntitiesCommand extends Command
const BUNCH_COUNT = 100;

/**
* @var EntityManager
* @var Registry
*/
private $em;
private $registry;

/**
* @var Config
Expand All @@ -44,14 +45,14 @@ class IndexEntitiesCommand extends Command
/**
* @param Config $config
* @param IndexProcessManager $indexProcessManager
* @param EntityManager $em
* @param Registry $registry
*/
public function __construct(Config $config, IndexProcessManager $indexProcessManager, EntityManager $em)
public function __construct(Config $config, IndexProcessManager $indexProcessManager, Registry $registry)
{
$this->config = $config;
$this->indexProcessManager = $indexProcessManager;
$this->possibleEntityTypes = array_keys($this->getAssocEntitiesClasses());
$this->em = $em;
$this->registry = $registry;

parent::__construct();
}
Expand Down Expand Up @@ -105,10 +106,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
* @param string $entityClass
* @param int|null $id
* @throws \Exception
* @throws \LogicException
*/
private function indexEntityClass($entityClass, $id = null)
{
$repository = $this->em->getRepository($entityClass);
$em = $this->registry->getManagerForClass($entityClass);
if (!$em instanceof EntityManager) {
throw new \LogicException('EntityManager must be instance of EntityManager');
}

$repository = $em->getRepository($entityClass);
if ($id) {
$entity = $repository->find($id);
if (!$entity) {
Expand All @@ -118,21 +125,23 @@ private function indexEntityClass($entityClass, $id = null)
}
$this->processEntity($entity);
} else {
$this->processRepository($repository);
$this->processRepository($repository, $em);
}
}

/**
* @param EntityRepository $repository
* @param EntityManager $em
*/
private function processRepository(EntityRepository $repository)
private function processRepository(EntityRepository $repository, EntityManager $em)
{
$offset = 0;
while ($entities = $repository->findBy([],['id'=> 'asc'], self::BUNCH_COUNT, $offset)) {
foreach ($entities as $entity) {
$this->processEntity($entity);
}
$this->em->clear($repository->getClassName());

$em->clear($repository->getClassName());
$offset += self::BUNCH_COUNT;
}
}
Expand Down
24 changes: 10 additions & 14 deletions Manager/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,14 @@ public function __construct(
$this->registry = $registry;
}

/**
* @param object $entity
* @throws \InvalidArgumentException
*/
public function persist($entity)
{
$this->getEm($entity)->persist($entity);
}

/**
* @param object[]|object $entity
* @throws \LogicException
* @throws \Doctrine\ORM\ORMException
* @throws \InvalidArgumentException
* @throws \LogicException
* @throws \Exception
*/
public function flush($entity = null)
public function flush($entity)
{
if (!is_object($entity) && !is_array($entity)) {
throw new \InvalidArgumentException('Entity must be an object or array of objects');
Expand All @@ -61,6 +54,7 @@ public function flush($entity = null)
/** @var \Doctrine\ORM\EntityManager $em */
$em = $this->getEm($object);
try {
$em->persist($object);
$em->flush($object);
} catch (\Exception $e) {
if (!$em->isOpen()) {
Expand All @@ -69,12 +63,14 @@ public function flush($entity = null)
);
}

$object = $em->getRepository(get_class($object))->find($object->getId());
if ($object) {
$this->indexProcessManager->reindex($object);
$previousObject = $em->getRepository(get_class($object))->find($object->getId());
if ($previousObject) {
$this->indexProcessManager->reindex($previousObject);
} else {
$this->indexProcessManager->remove($object);
}

throw $e;
}
}
}
Expand Down
21 changes: 15 additions & 6 deletions Query/Hydrator/HydratorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,48 @@

namespace Mdiyakov\DoctrineSolrBundle\Query\Hydrator;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\EntityManager;
use Mdiyakov\DoctrineSolrBundle\Config\Config;

class HydratorBuilder
{
/**
* @var EntityManager
* @var Registry
*/
private $em;
private $registry;

/**
* @var Config
*/
private $config;

/**
* @param EntityManager $em
* @param Registry $registry
* @param Config $config
*/
public function __construct(EntityManager $em, Config $config)
public function __construct(Registry $registry, Config $config)
{
$this->em = $em;
$this->registry = $registry;
$this->config = $config;
}

/**
* @param string $entityClass
* @return SelectQueryHydrator
* @throws \LogicException
*/
public function buildSelectQueryHydratorByClass($entityClass)
{
$em = $this->registry->getManagerForClass($entityClass);
if (!$em instanceof EntityManager) {
throw new \LogicException(
'EntityManager must be instance of \Doctrine\ORM\EntityManager'
);
}

return new SelectQueryHydrator(
$this->em->getRepository($entityClass),
$em->getRepository($entityClass),
$this->config->getSchemaByEntityClass($entityClass),
$this->config->getEntityConfig($entityClass)
);
Expand Down
9 changes: 6 additions & 3 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ services:
arguments:
- '@mdiyakov_doctrine_solr.config.config'
- '@mdiyakov_doctrine_solr.manager.index_process_manager'
- '@doctrine.orm.default_entity_manager'
- '@doctrine'
tags:
- { name: console.command }

Expand All @@ -50,15 +50,15 @@ services:
arguments:
- '@mdiyakov_doctrine_solr.config.config'
- '@mdiyakov_doctrine_solr.query.update_builder'
- '@doctrine.orm.default_entity_manager'
- '@doctrine'
tags:
- { name: console.command }

mdiyakov_doctrine_solr.query.hydrator.builder:
class: Mdiyakov\DoctrineSolrBundle\Query\Hydrator\HydratorBuilder
public: false
arguments:
- '@doctrine.orm.default_entity_manager'
- '@doctrine'
- '@mdiyakov_doctrine_solr.config.config'

mdiyakov_doctrine_solr.query.select_builder:
Expand Down Expand Up @@ -98,6 +98,9 @@ services:
- '@mdiyakov_doctrine_solr.manager.index_process_manager'
- '@doctrine'

ds.entity_manager:
alias: mdiyakov_doctrine_solr.manager.entity_manager

ds.suggester:
alias: mdiyakov_doctrine_solr.manager.suggester

Expand Down

0 comments on commit 3178196

Please sign in to comment.