public function editAction() { // Check if id set. $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { $this->flashMessenger()->addErrorMessage('Blogpost id doesn\'t set'); return $this->redirect()->toRoute('blog'); } // Create form. $form = new \MyBlog\Form\BlogPostForm(); $form->get('submit')->setValue('Save'); $request = $this->getRequest(); if (!$request->isPost()) { $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $post = $objectManager->getRepository('\\MyBlog\\Entity\\BlogPost')->findOneBy(array('id' => $id)); if (!$post) { $this->flashMessenger()->addErrorMessage(sprintf('Blogpost with id %s doesn\'t exists', $id)); return $this->redirect()->toRoute('blog'); } // Fill form data. $form->bind($post); return array('form' => $form, 'id' => $id, 'post' => $post); } else { $form->setData($request->getPost()); if ($form->isValid()) { $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $data = $form->getData(); $id = $data['id']; try { $blogpost = $objectManager->find('\\MyBlog\\Entity\\BlogPost', $id); } catch (\Exception $ex) { return $this->redirect()->toRoute('blog', array('action' => 'index')); } $blogpost->exchangeArray($form->getData()); $objectManager->persist($blogpost); $objectManager->flush(); $message = 'Blogpost succesfully saved!'; $this->flashMessenger()->addMessage($message); // Redirect to list of blogposts return $this->redirect()->toRoute('blog'); } else { $message = 'Error while saving blogpost'; $this->flashMessenger()->addErrorMessage($message); return array('form' => $form, 'id' => $id); } } }
public function editAction() { $form = new \MyBlog\Form\BlogPostForm(); $form->get('submit')->setValue('Сохранить'); $request = $this->getRequest(); if (!$request->isPost()) { $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { $this->flashMessenger()->addErrorMessage('Пост не существует'); return $this->redirect()->toRoute('blog'); } $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $post = $objectManager->getRepository('\\MyBlog\\Entity\\BlogPost')->findOneBy(array('id' => $id)); if (!$post) { $this->flashMessenger()->addErrorMessage(sprintf('Пост с id %s не существует', $id)); return $this->redirect()->toRoute('blog'); } $form->bind($post); return array('form' => $form); } else { $form->setData($request->getPost()); if ($form->isValid()) { $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $data = $form->getData(); $id = $data['id']; try { $blogpost = $objectManager->find('\\MyBlog\\Entity\\BlogPost', $id); } catch (\Exception $ex) { return $this->redirect()->toRoute('blog', array('action' => 'index')); } $blogpost->exchangeArray($form->getData()); $objectManager->persist($blogpost); $objectManager->flush(); $message = 'Пост сохранен!'; $this->flashMessenger()->addMessage($message); return $this->redirect()->toRoute('blog'); } else { $message = 'Ошибка при сохранении'; $this->flashMessenger()->addErrorMessage($message); } } }