示例#1
0
 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();
     }
 }
示例#2
0
 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');
     }
 }
示例#3
0
 public function testUpdate_WithInValidDataInFilter()
 {
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $filterMock = $this->getMock(InputFilterInterface::class);
     $filterMock->expects($this->once())->method('isValid')->will($this->returnValue(false));
     $this->setExpectedException('LogicException');
     $this->testedObj->update($bookEntity, $filterMock);
 }