I use symfony 2.3 and php doctrine 2.
The program has the following models:
- entity Order - a typical customer order
- entity BadOrderEntry(fields: id, order - unidirectional one-to-one relationship with Order, createdAt)
- factory BadOrderEntryFactory for creation entity BadOrderEntry
- repository BadOrderEntryRepository for search methods of entity BadOrderEntry
- manager BadOrderEntryManager for save/edit/delete methods of entity BadOrderEntry
AND MAIN CLASS BadOrderList - list of bad orders, 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 methods add and remove must be performed in the transactions.
Transaction code in PHP Docrine 2 looks like this:
<?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 BadOrderList?
I spent a lot of time and removed depending on the database(and correspondingly PHP Doctrine 2), and again to create it? Is now dependence is hidden in classes BadOrderEntryRepository and BadOrderEntryManager.
How to hide the dependence on the transaction mechanism in class BadOrderList?
Manager::add
anddelete
I also suggest you to rethink your design. It's not really nice. Make your model persistent independent. – Annikaanniken