Skip to content

Commit bbb684e

Browse files
committed
- entity manager single argument
- entity manager docs
1 parent 4c50bf6 commit bbb684e

File tree

2 files changed

+26
-42
lines changed

2 files changed

+26
-42
lines changed

Manager/EntityManager.php

+23-29
Original file line numberDiff line numberDiff line change
@@ -30,53 +30,47 @@ public function __construct(
3030
}
3131

3232
/**
33-
* @param object[]|object $entity
33+
* @param object $entity
3434
* @throws \Doctrine\ORM\ORMException
3535
* @throws \InvalidArgumentException
3636
* @throws \LogicException
3737
* @throws \Exception
3838
*/
3939
public function flush($entity)
4040
{
41-
if (!is_object($entity) && !is_array($entity)) {
42-
throw new \InvalidArgumentException('Entity must be an object or array of objects');
41+
if (!is_object($entity)) {
42+
throw new \InvalidArgumentException('Entity must be an object');
4343
}
4444

45-
if (!is_array($entity)) {
46-
$entity = [$entity];
45+
if (!method_exists($entity, 'getId')) {
46+
throw new \LogicException('Entity must have method "getId" to handle rollback');
4747
}
4848

49-
foreach ($entity as $object) {
50-
if (!method_exists($object, 'getId')) {
51-
throw new \LogicException('Entity must have method "getId" to handle rollback');
49+
/** @var \Doctrine\ORM\EntityManager $em */
50+
$em = $this->getEm($entity);
51+
try {
52+
$em->persist($entity);
53+
$em->flush($entity);
54+
} catch (\Exception $e) {
55+
if (!$em->isOpen()) {
56+
$em = $em->create(
57+
$em->getConnection(), $em->getConfiguration()
58+
);
5259
}
5360

54-
/** @var \Doctrine\ORM\EntityManager $em */
55-
$em = $this->getEm($object);
56-
try {
57-
$em->persist($object);
58-
$em->flush($object);
59-
} catch (\Exception $e) {
60-
if (!$em->isOpen()) {
61-
$em = $em->create(
62-
$em->getConnection(), $em->getConfiguration()
63-
);
64-
}
65-
66-
$previousObject = $em->getRepository(get_class($object))->find($object->getId());
67-
if ($previousObject) {
68-
$this->indexProcessManager->reindex($previousObject);
69-
} else {
70-
$this->indexProcessManager->remove($object);
71-
}
72-
73-
throw $e;
61+
$previousEntity = $em->getRepository(get_class($entity))->find($entity->getId());
62+
if ($previousEntity) {
63+
$this->indexProcessManager->reindex($previousEntity);
64+
} else {
65+
$this->indexProcessManager->remove($entity);
7466
}
67+
68+
throw $e;
7569
}
7670
}
7771

7872
/**
79-
* @param $entity
73+
* @param object $entity
8074
* @throws \InvalidArgumentException
8175
* @return EntityManagerInterface
8276
*/

Resources/doc/entity_manager.md

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

33
**DoctrineSolrBundle** provides an entity manager (**\Mdiyakov\DoctrineSolrBundle\Manager\EntityManager**) to handle the case when an exception was triggered during flushing entity in database.
44

5-
During flushing an entity in database there is probability the exception will be thrown after the changes were successfully saved in solr. It can be caused by other entity listeners or something issues with database. In this case inconsistency can be broken beetwen database and solr because in database the changes will be rollback but in solr will be kept.
5+
During flushing an entity in database there is probability the exception will be thrown after the changes were successfully saved in solr. It can be caused by other entity listeners or something issues with database. In this case consistency can be broken beetwen database and solr because in database the changes will be rollback but in solr will be kept.
66

77
To rollback the changes in solr in this case you can use the following approach:
88

@@ -14,18 +14,8 @@ $myEntity = new MyEntity();
1414
1515
$dsEm ->flush($myEntity);
1616
```
17-
In this case if any exception will be thrown in entity listener chain for example the solr changes also will be rollback.
17+
In this case if any exception will be thrown (for example in entity listener chain) the solr changes also will be rollback.
1818

19-
You can use it with a few entities:
20-
```
21-
$dsEm = $this->get('ds.entity_manager');
22-
$myEntity = new MyEntity();
23-
$mySecondEntity = new MySecondEntity();
24-
...
25-
26-
$dsEm ->flush([$myEntity,$mySecondEntity]);
27-
28-
```
2919

30-
> Pay attention 'ds.entity_manager' doesn't have interface like \Doctrine\ORM\EntityManager. It has only "flush"" method with mandatory argument
20+
> Pay attention 'ds.entity_manager' doesn't have interface like \Doctrine\ORM\EntityManager. It has only "flush"" method with mandatory argument.
3121
> Also you don't need to call 'persist' method

0 commit comments

Comments
 (0)