Skip to content

Commit 3178196

Browse files
committed
wrapper for $em - fixed case with entity creation
replace entity manager by registry
1 parent 872d362 commit 3178196

File tree

5 files changed

+57
-39
lines changed

5 files changed

+57
-39
lines changed

Command/ClearIndexCommand.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Mdiyakov\DoctrineSolrBundle\Command;
44

5-
use Doctrine\ORM\EntityManager;
5+
use Doctrine\Bundle\DoctrineBundle\Registry;
66
use Doctrine\ORM\EntityNotFoundException;
77
use Mdiyakov\DoctrineSolrBundle\Config\Config;
88
use Mdiyakov\DoctrineSolrBundle\Exception\EntityNotIndexedException;
@@ -31,25 +31,25 @@ class ClearIndexCommand extends Command
3131
private $updateQueryBuilder;
3232

3333
/**
34-
* @var EntityManager
34+
* @var Registry
3535
*/
36-
private $em;
36+
private $registry;
3737

3838
/**
3939
* @param Config $config
4040
* @param UpdateQueryBuilder $updateQueryBuilder
41-
* @param EntityManager $em
41+
* @param Registry $registry
4242
*/
4343
public function __construct(
4444
Config $config,
4545
UpdateQueryBuilder $updateQueryBuilder,
46-
EntityManager $em
46+
Registry $registry
4747
)
4848
{
4949
$this->config = $config;
5050
$this->possibleEntityTypes = array_keys($this->getAssocEntitiesClasses());
5151
$this->updateQueryBuilder = $updateQueryBuilder;
52-
$this->em = $em;
52+
$this->registry = $registry;
5353

5454
parent::__construct();
5555
}
@@ -135,6 +135,7 @@ private function deleteByEntityConfig($entityConfig, $id = null)
135135
$schemaName = $entityConfig['schema'];
136136
$updateQuery = $this->updateQueryBuilder->buildUpdateQueryBySchemaName($schemaName);
137137
$schema = $this->config->getSchemaByName($schemaName);
138+
$em = $this->registry->getManagerForClass($entityConfig['class']);
138139

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

152153
if (!$entity) {

Command/IndexEntitiesCommand.php

+18-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Mdiyakov\DoctrineSolrBundle\Command;
44

5+
use Doctrine\Bundle\DoctrineBundle\Registry;
56
use Doctrine\ORM\EntityManager;
67
use Doctrine\ORM\EntityRepository;
78
use Symfony\Component\Console\Input\InputArgument;
@@ -17,9 +18,9 @@ class IndexEntitiesCommand extends Command
1718
const BUNCH_COUNT = 100;
1819

1920
/**
20-
* @var EntityManager
21+
* @var Registry
2122
*/
22-
private $em;
23+
private $registry;
2324

2425
/**
2526
* @var Config
@@ -44,14 +45,14 @@ class IndexEntitiesCommand extends Command
4445
/**
4546
* @param Config $config
4647
* @param IndexProcessManager $indexProcessManager
47-
* @param EntityManager $em
48+
* @param Registry $registry
4849
*/
49-
public function __construct(Config $config, IndexProcessManager $indexProcessManager, EntityManager $em)
50+
public function __construct(Config $config, IndexProcessManager $indexProcessManager, Registry $registry)
5051
{
5152
$this->config = $config;
5253
$this->indexProcessManager = $indexProcessManager;
5354
$this->possibleEntityTypes = array_keys($this->getAssocEntitiesClasses());
54-
$this->em = $em;
55+
$this->registry = $registry;
5556

5657
parent::__construct();
5758
}
@@ -105,10 +106,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
105106
* @param string $entityClass
106107
* @param int|null $id
107108
* @throws \Exception
109+
* @throws \LogicException
108110
*/
109111
private function indexEntityClass($entityClass, $id = null)
110112
{
111-
$repository = $this->em->getRepository($entityClass);
113+
$em = $this->registry->getManagerForClass($entityClass);
114+
if (!$em instanceof EntityManager) {
115+
throw new \LogicException('EntityManager must be instance of EntityManager');
116+
}
117+
118+
$repository = $em->getRepository($entityClass);
112119
if ($id) {
113120
$entity = $repository->find($id);
114121
if (!$entity) {
@@ -118,21 +125,23 @@ private function indexEntityClass($entityClass, $id = null)
118125
}
119126
$this->processEntity($entity);
120127
} else {
121-
$this->processRepository($repository);
128+
$this->processRepository($repository, $em);
122129
}
123130
}
124131

125132
/**
126133
* @param EntityRepository $repository
134+
* @param EntityManager $em
127135
*/
128-
private function processRepository(EntityRepository $repository)
136+
private function processRepository(EntityRepository $repository, EntityManager $em)
129137
{
130138
$offset = 0;
131139
while ($entities = $repository->findBy([],['id'=> 'asc'], self::BUNCH_COUNT, $offset)) {
132140
foreach ($entities as $entity) {
133141
$this->processEntity($entity);
134142
}
135-
$this->em->clear($repository->getClassName());
143+
144+
$em->clear($repository->getClassName());
136145
$offset += self::BUNCH_COUNT;
137146
}
138147
}

Manager/EntityManager.php

+10-14
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,14 @@ public function __construct(
2929
$this->registry = $registry;
3030
}
3131

32-
/**
33-
* @param object $entity
34-
* @throws \InvalidArgumentException
35-
*/
36-
public function persist($entity)
37-
{
38-
$this->getEm($entity)->persist($entity);
39-
}
40-
4132
/**
4233
* @param object[]|object $entity
43-
* @throws \LogicException
34+
* @throws \Doctrine\ORM\ORMException
4435
* @throws \InvalidArgumentException
36+
* @throws \LogicException
37+
* @throws \Exception
4538
*/
46-
public function flush($entity = null)
39+
public function flush($entity)
4740
{
4841
if (!is_object($entity) && !is_array($entity)) {
4942
throw new \InvalidArgumentException('Entity must be an object or array of objects');
@@ -61,6 +54,7 @@ public function flush($entity = null)
6154
/** @var \Doctrine\ORM\EntityManager $em */
6255
$em = $this->getEm($object);
6356
try {
57+
$em->persist($object);
6458
$em->flush($object);
6559
} catch (\Exception $e) {
6660
if (!$em->isOpen()) {
@@ -69,12 +63,14 @@ public function flush($entity = null)
6963
);
7064
}
7165

72-
$object = $em->getRepository(get_class($object))->find($object->getId());
73-
if ($object) {
74-
$this->indexProcessManager->reindex($object);
66+
$previousObject = $em->getRepository(get_class($object))->find($object->getId());
67+
if ($previousObject) {
68+
$this->indexProcessManager->reindex($previousObject);
7569
} else {
7670
$this->indexProcessManager->remove($object);
7771
}
72+
73+
throw $e;
7874
}
7975
}
8076
}

Query/Hydrator/HydratorBuilder.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,48 @@
22

33
namespace Mdiyakov\DoctrineSolrBundle\Query\Hydrator;
44

5+
use Doctrine\Bundle\DoctrineBundle\Registry;
56
use Doctrine\ORM\EntityManager;
67
use Mdiyakov\DoctrineSolrBundle\Config\Config;
78

89
class HydratorBuilder
910
{
1011
/**
11-
* @var EntityManager
12+
* @var Registry
1213
*/
13-
private $em;
14+
private $registry;
1415

1516
/**
1617
* @var Config
1718
*/
1819
private $config;
1920

2021
/**
21-
* @param EntityManager $em
22+
* @param Registry $registry
2223
* @param Config $config
2324
*/
24-
public function __construct(EntityManager $em, Config $config)
25+
public function __construct(Registry $registry, Config $config)
2526
{
26-
$this->em = $em;
27+
$this->registry = $registry;
2728
$this->config = $config;
2829
}
2930

3031
/**
3132
* @param string $entityClass
3233
* @return SelectQueryHydrator
34+
* @throws \LogicException
3335
*/
3436
public function buildSelectQueryHydratorByClass($entityClass)
3537
{
38+
$em = $this->registry->getManagerForClass($entityClass);
39+
if (!$em instanceof EntityManager) {
40+
throw new \LogicException(
41+
'EntityManager must be instance of \Doctrine\ORM\EntityManager'
42+
);
43+
}
44+
3645
return new SelectQueryHydrator(
37-
$this->em->getRepository($entityClass),
46+
$em->getRepository($entityClass),
3847
$this->config->getSchemaByEntityClass($entityClass),
3948
$this->config->getEntityConfig($entityClass)
4049
);

Resources/config/services.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ services:
4141
arguments:
4242
- '@mdiyakov_doctrine_solr.config.config'
4343
- '@mdiyakov_doctrine_solr.manager.index_process_manager'
44-
- '@doctrine.orm.default_entity_manager'
44+
- '@doctrine'
4545
tags:
4646
- { name: console.command }
4747

@@ -50,15 +50,15 @@ services:
5050
arguments:
5151
- '@mdiyakov_doctrine_solr.config.config'
5252
- '@mdiyakov_doctrine_solr.query.update_builder'
53-
- '@doctrine.orm.default_entity_manager'
53+
- '@doctrine'
5454
tags:
5555
- { name: console.command }
5656

5757
mdiyakov_doctrine_solr.query.hydrator.builder:
5858
class: Mdiyakov\DoctrineSolrBundle\Query\Hydrator\HydratorBuilder
5959
public: false
6060
arguments:
61-
- '@doctrine.orm.default_entity_manager'
61+
- '@doctrine'
6262
- '@mdiyakov_doctrine_solr.config.config'
6363

6464
mdiyakov_doctrine_solr.query.select_builder:
@@ -98,6 +98,9 @@ services:
9898
- '@mdiyakov_doctrine_solr.manager.index_process_manager'
9999
- '@doctrine'
100100

101+
ds.entity_manager:
102+
alias: mdiyakov_doctrine_solr.manager.entity_manager
103+
101104
ds.suggester:
102105
alias: mdiyakov_doctrine_solr.manager.suggester
103106

0 commit comments

Comments
 (0)