public function save(\Api\Entity\Post $post) { $hydrator = $this->getHydrator(); $action = null; $postData = array('title' => $post->getTitle(), 'description' => $post->getDescription()); if ($post->getId()) { $action = new Update('posts'); $action->set($postData); $action->where(array('id = ?' => $post->getId())); } else { $postData['author_id'] = $post->getAuthorId(); $action = new Insert('posts'); $action->values($postData); } $sql = new Sql($this->getAdaptor()); $statement = $sql->prepareStatementForSqlObject($action); $result = $statement->execute(); if ($result instanceof ResultInterface) { if ($pk = $result->getGeneratedValue()) { $post->setId($pk); } return $this->getPost($post->getId()); } throw new \Exception('something went wrong.Please try again later'); }
public function addAction() { $response = array(); $service = $this->getPostService(); $request = $this->getRequest(); $postForm = new PostForm(); $userModel = new Post(); $postForm->setInputFilter($userModel->inputFilter()); $postForm->setData($request->getPost()); if ($postForm->isValid()) { try { $userModel->exchangeArray($postForm->getData()); $model = $service->save($userModel); $response = array('status' => true, 'data' => $model, 'message' => ''); } catch (\Exception $e) { $response = array('status' => false, 'exception' => 'SERVICE_ERROR', 'data' => null, 'message' => $e->getMessage()); } } else { $response = array('status' => false, 'exception' => 'INVALID_DATA', 'data' => $postForm->getMessages(), 'message' => 'Please Provide valid data'); } return new JsonModel($response); }