public function createAction() { $user = $this->identity(); $request = $this->getRequest(); $form = new MessageForm(); if ($request->isPost()) { $data = $request->getPost(); $form->setData($data); if ($form->isValid()) { $data = $form->getData(); $message = new Message($user, $data['subject'], $data['body']); foreach ($data['recipients'] as $recipient) { $receiver = $this->dm->getRepository('User\\Document\\User')->findOneBy(array('id' => $recipient['id'])); $message->setRecipients($recipient); } $this->dm->persist($message); $this->dm->persist($user); $this->dm->flush(); $this->flashMessenger()->addSuccessMessage("Message sent successfully!"); return $this->redirect()->toRoute('user_dashboard/messages'); } else { $this->flashMessenger()->addErrorMessage("There was a problem creating your message! Review the form and try again!"); return $this->redirect()->toRoute('user_dashboard/messages'); } } }
/** * Create a new resource * * @param mixed $data * @return mixed */ public function create($data) { $matches = $this->getEvent()->getRouteMatch(); $params = $matches->getParams(); $author = $this->dm->getRepository('User\\Document\\User')->findOneById($params['uid']); $hydrator = new DoctrineHydrator($this->dm); $form = new MessageForm(); $form->setData($data); if ($form->isValid()) { $message = new Message($author); $newMessage = $hydrator->hydrate($form->getData(), $message); $this->dm->persist($newMessage); $this->dm->flush(); return new JsonModel(['Success' => ['message' => 'Your message was created successfully!', '_link' => $this->url()->fromRoute('api.user.message') . $newMessage['id']]]); } else { return new JsonModel(['Error' => ['message' => 'Invalid Request Object!', 'request' => $data]]); } }