class MyRepository
{
private $storage;
public function get($id) {
$data = $this->storage->fetch($id);
$object = MyObject::createFromArray($data);
//... Ещё какой-то код, связи например
return $object;
}
public function save(MyObject $object) {
$data = [];
// Тут свой код, создаем например массив $data, все что нужно для сохраниение в наш $storage
$this->storage->save($data);
}
// Аналогично удаление, выборки, если нужно
}
protected function flush()
{
$em = $this->get('doctrine.orm.entity_manager');
$connection = $em->getConnection();
try {
$connection->beginTransaction();
$em->flush();
$connection->commit();
} catch (\Exception $exc) {
$connection->rollBack();
throw $exc;
}
}
services:
infrastructure.doctrine_domain_events_handler:
class: Infrastructure\BlaBlaNamespace\DomainEvent\DoctrineDomainEventsHandler
arguments: ["@event_dispatcher"]
tags:
- { name: doctrine.event_listener, event: postFlush }
class DoctrineDomainEventsHandler
{
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
//....
public function postFlush(PostFlushEventArgs $args)
{
$em = $args->getEntityManager();
$identityMap = $em->getUnitOfWork()->getIdentityMap();
foreach ($identityMap as $entities) {
foreach ($entities as $entity) {
if ($entity instanceof DomainEventsAwareInterface) {
$events = $entity->releaseEvents();
foreach ($events as $event) {
$this->dispatcher->dispatch($event->getName(), $event);
}
}
}
}
}
}