/** * create * * @param mixed $data * * @return ApiJsonModel|\Zend\Stdlib\ResponseInterface */ public function create($data) { if (!$this->rcmIsAllowed('sites', 'admin')) { $this->getResponse()->setStatusCode(Response::STATUS_CODE_401); return $this->getResponse(); } $userId = $this->getEvent()->getRouteMatch()->getParam('userId', null); $newUserMessage = new UserMessage($userId); $newUserMessage->populate($data, ['id', 'dateViewed', 'message']); // @todo Should we force creation???? $newMessage = new Message(); $newMessage->populate($data['message'], ['id', 'dateCreated']); $newUserMessage->setMessage($newMessage); $entityManager = $this->getEntityManager(); try { $entityManager->persist($newUserMessage); $entityManager->flush(); } catch (\Exception $e) { return new ApiJsonModel(null, 1, $e->getMessage()); } return new ApiJsonModel($newUserMessage); }