-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from gmorel/feature/behat3_multiple_entity_man…
…agers Now work with several entity managers (Behat3)
- Loading branch information
Showing
4 changed files
with
91 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Rezzza\AliceExtension\Doctrine; | ||
|
||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Nelmio\Alice\ORM\Doctrine; | ||
|
||
class ORMPersister extends Doctrine | ||
{ | ||
protected $registry; | ||
protected $flush; | ||
|
||
public function __construct(ManagerRegistry $registry, $doFlush = true) | ||
{ | ||
$this->registry = $registry; | ||
$this->flush = $doFlush; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function persist(array $objects) | ||
{ | ||
$managersToFlush = []; | ||
|
||
foreach ($objects as $object) { | ||
$managerName = $this->findManagerForClass(get_class($object)); | ||
$this->registry->getManager($managerName)->persist($object); | ||
|
||
$managersToFlush[$managerName] = $managerName; | ||
} | ||
|
||
if ($this->flush) { | ||
foreach ($managersToFlush as $managerName) { | ||
$this->registry->getManager($managerName)->flush(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function find($class, $id) | ||
{ | ||
$managerName = $this->findManagerForClass($class); | ||
$entity = $this->registry->getManager($managerName)->find($class, $id); | ||
|
||
if (!$entity) { | ||
throw new \UnexpectedValueException('Entity with Id ' . $id . ' and Class ' . $class . ' not found'); | ||
} | ||
|
||
return $entity; | ||
} | ||
|
||
/** | ||
* @param string $entityClass | ||
* @return string | ||
* @throws \Doctrine\Common\Persistence\Mapping\MappingException | ||
*/ | ||
private function findManagerForClass($entityClass) | ||
{ | ||
foreach ($this->registry->getManagers() as $name => $manager) { | ||
if ($manager->getMetadataFactory()->hasMetadataFor($entityClass)) { | ||
return $name; | ||
} | ||
} | ||
|
||
throw new \Doctrine\Common\Persistence\Mapping\MappingException(sprintf('Entity class "%s" not found in managers.', $entityClass)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters