public function indexAction() { /** @var \Zend\Http\Request $request */ $request = $this->getRequest(); $id = $this->params()->fromRoute('id', null); try { $bookEntity = $this->service->getById($id); $this->form->get('submit')->setValue('Update'); if ($request->isPost()) { $this->form->setData($request->getPost()->toArray() ?: []); if ($this->form->isValid()) { $this->service->update($bookEntity, $this->form->getInputFilter()); $this->flashMessenger()->addSuccessMessage('Books saved successfully!'); } else { $this->flashMessenger()->addErrorMessage('Please fill form correctly'); } } else { $data = $this->service->extractEntity($bookEntity); $this->form->setData($data); } return ['form' => $this->form]; } catch (\Exception $e) { $this->flashMessenger()->addErrorMessage($e->getMessage()); return $this->redirect()->toRoute('library/books'); } }
public function indexAction() { /** * @var Request $request * @var Response $response */ $request = $this->getRequest(); $response = $this->getResponse(); $id = $this->params()->fromRoute('id', null); try { $bookEntity = $this->service->getById($id); $this->filter->setData($request->getPost()->toArray()); if ($this->filter->isValid()) { $bookEntity = $this->service->update($bookEntity, $this->filter); return new JsonModel($this->service->extractEntity($bookEntity)); } else { $messages = $this->filter->getMessages(); $response->setStatusCode(Response::STATUS_CODE_400); return new JsonModel(['error' => ['messages' => $messages]]); } } catch (EntityNotFoundException $e) { throw new Exception\NotFoundException(); } catch (\PDOException $e) { throw new Exception\PDOServiceUnavailableException(); } }
public function indexAction() { $id = $this->params()->fromRoute('id', null); try { $bookEntity = $this->service->getById($id); $data = $this->service->extractEntity($bookEntity); return new JsonModel($data); } catch (EntityNotFoundException $e) { throw new Exception\NotFoundException(); } catch (\PDOException $e) { throw new Exception\PDOServiceUnavailableException(); } }
public function indexAction() { $id = $this->params()->fromRoute('id', null); try { $bookEntity = $this->service->getById($id); $this->service->delete($bookEntity); return new JsonModel(['data' => "Books with id {$id} has been deleted"]); } catch (EntityNotFoundException $e) { throw new Exception\NotFoundException(); } catch (\PDOException $e) { throw new Exception\PDOServiceUnavailableException(); } }
public function testGetById_WithNotFoundException() { $id = 12; $this->bookRepositoryMock->expects($this->once())->method('find')->with($id)->will($this->returnValue(null)); $this->setExpectedException('Doctrine\\ORM\\EntityNotFoundException'); $this->testedObj->getById($id); }