I am using symfony 2.3 and php doctrine 2.
The program has the following models:
- entity Order - typical sales order
- BadOrderEntry object (fields: id, order - one-to-one unidirectional communication with Order, createdAt)
- factory BadOrderEntryFactory for the creation object BadOrderEntry
- BadOrderEntryRepository repository for BadOrderEntry object search methods
- BadOrderEntryManager manager for saving / editing / deleting BadOrderEntry entity methods
AND THE MAIN CLASS BadOrderList - a list of failed orders, the code of this class:
private $factory; private $repository; private $manager; public function __construct( BadOrderEntryFactory $f, BadOrderEntryRepository $r, BadOrderEntryManager $m ) { $this->factory = $f; $this->repository = $r; $this->manager = $m; } public function has(Order $order) { return $this->repository->existsByOrder($order); } public function add(Order $order) { if (! $this->has($order)) { $entry = $this->factory->create($order); $this->manager->save($entry); } } public function remove(Order $order) { $entry = $this->repository->findOneByOrder($order); if ($entry !== null) { $this->manager->delete($entry); } }
I really like the design of this class. I thought a lot about it. Everything is wonderful. BUT! There is one problem: operations in the add and remove methods must be performed in transactions.
The transaction code in PHP Docrine 2 is as follows:
<?php $em->getConnection()->beginTransaction(); try { //... do some work $em->getConnection()->commit(); } catch (Exception $e) { $em->getConnection()->rollback(); throw $e; }
But how can I call this code inside a BadOrderList?
I spent a lot of time and deleted depending on the database (and, accordingly, PHP Doctrine 2), and again to create it? The dependency is now hidden in the BadOrderEntryRepository and BadOrderEntryManager classes.
How to hide the dependence on the transaction mechanism in the BadOrderList class?
php orm symfony doctrine2 datamapper
stalxed
source share