diff --git a/spec/Phpro/SmartCrud/Gateway/DoctrineCrudGatewaySpec.php b/spec/Phpro/SmartCrud/Gateway/DoctrineCrudGatewaySpec.php index f161d8f..ca4c851 100644 --- a/spec/Phpro/SmartCrud/Gateway/DoctrineCrudGatewaySpec.php +++ b/spec/Phpro/SmartCrud/Gateway/DoctrineCrudGatewaySpec.php @@ -176,7 +176,6 @@ public function it_should_delete_entity($objectManager, $entity) public function it_should_not_delete_invalid_entity($objectManager, $entity) { $objectManager->flush()->willThrow('\Exception'); - - $this->delete($entity, array())->shouldReturn(false); + $this->shouldThrow('\Exception')->duringDelete($entity, array()); } } diff --git a/spec/Phpro/SmartCrud/Service/SmartServiceResultSpec.php b/spec/Phpro/SmartCrud/Service/SmartServiceResultSpec.php index 247b0ba..3f61acb 100644 --- a/spec/Phpro/SmartCrud/Service/SmartServiceResultSpec.php +++ b/spec/Phpro/SmartCrud/Service/SmartServiceResultSpec.php @@ -50,4 +50,12 @@ public function it_should_have_a_list() $this->setList($list)->shouldReturn($this); $this->getList()->shouldReturn($list); } + + public function it_should_have_messages() + { + $messages = array('msg1'); + $this->setMessages($messages)->shouldReturn($this); + $this->addMessage('msg2')->shouldReturn($this); + $this->getMessages()->shouldReturn(array('msg1', 'msg2')); + } } diff --git a/src/Phpro/SmartCrud/Gateway/DoctrineCrudGateway.php b/src/Phpro/SmartCrud/Gateway/DoctrineCrudGateway.php index d560654..f54844b 100644 --- a/src/Phpro/SmartCrud/Gateway/DoctrineCrudGateway.php +++ b/src/Phpro/SmartCrud/Gateway/DoctrineCrudGateway.php @@ -24,7 +24,6 @@ class DoctrineCrudGateway implements ObjectManagerAwareInterface, CrudGatewayInterface { - const TYPE_ORM = 'ORM'; const TYPE_ODM = 'ODM'; @@ -133,8 +132,8 @@ public function update($entity, $parameters) /** * @param $entity * @param $id - * * @return bool + * @throws \Exception */ public function delete($entity, $id) { @@ -143,7 +142,7 @@ public function delete($entity, $id) $em->remove($entity); $em->flush(); } catch (\Exception $e) { - return false; + throw new \Exception($e->getMessage()); } return true; diff --git a/src/Phpro/SmartCrud/Service/DeleteService.php b/src/Phpro/SmartCrud/Service/DeleteService.php index 061e25a..9745491 100644 --- a/src/Phpro/SmartCrud/Service/DeleteService.php +++ b/src/Phpro/SmartCrud/Service/DeleteService.php @@ -39,10 +39,16 @@ public function run($id = null, $data = null) } $em->trigger($this->createEvent(CrudEvent::BEFORE_DELETE, $entity)); - $deleted = $this->getGateway()->delete($entity, $data); + try { + $deleted = $this->getGateway()->delete($entity, $data); + $result->setSuccess($deleted); + } catch (\Exception $exception) { + $result->setSuccess(false); + $result->addMessage($exception->getMessage()); + return $result; + } $em->trigger($this->createEvent(CrudEvent::AFTER_DELETE, $entity)); - $result->setSuccess($deleted); return $result; } } diff --git a/src/Phpro/SmartCrud/Service/SmartServiceResult.php b/src/Phpro/SmartCrud/Service/SmartServiceResult.php index 05c6223..2353ee2 100644 --- a/src/Phpro/SmartCrud/Service/SmartServiceResult.php +++ b/src/Phpro/SmartCrud/Service/SmartServiceResult.php @@ -31,6 +31,11 @@ class SmartServiceResult */ private $list = array(); + /** + * @var array + */ + private $messages = array(); + /** * @param mixed|null $form */ @@ -109,4 +114,32 @@ public function isSuccessFull() { return $this->success; } + + /** + * @return array + */ + public function getMessages() + { + return $this->messages; + } + + /** + * @param $message + * @return $this + */ + public function addMessage($message) + { + $this->messages[] = $message; + return $this; + } + + /** + * @param array $messages + * @return $this + */ + public function setMessages($messages) + { + $this->messages = $messages; + return $this; + } }