public function editAction() { $id = $this->params('id'); $category = $this->getEntityManager()->getRepository('Blog\\Entity\\Category')->find($id); if (!$category) { throw new EntityNotFoundException('Entity Category not found'); } $form = new CategoryForm(); $form->setData((array) $category); $form->get('submit')->setAttribute('value', 'Modifier'); $request = $this->getRequest(); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $category = $this->getHydrator()->hydrate($form->getData(), $category); //Persist and flush entity Category $em = $this->getEntityManager(); $em->persist($category); $em->flush(); $eventManager = $this->getEventManager(); $eventManager->trigger('category.edit', null, ['category_id' => $category->getId(), 'category_name' => $category->getName(), 'user_id' => $this->zfcUserAuthentication()->getIdentity()->getId(), 'user_email' => $this->zfcUserAuthentication()->getIdentity()->getEmail()]); //Add flash message $this->flashMessenger()->addMessage('La catégorie ' . $form->getData()['name'] . ' a été modifié.'); //Redirection return $this->redirect()->toRoute('admin/categories'); } } return new ViewModel(['form' => $form, 'category' => $category]); }
public function editAction() { $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('category', array('action' => 'add')); } // Get the Category with the specified id. An exception is thrown // if it cannot be found, in which case go to the index page. try { $category = $this->getCategoryTable()->get($id); } catch (\Exception $ex) { return $this->redirect()->toRoute('category', array('action' => 'index')); } $form = new CategoryForm(); $form->bind($category); $request = $this->getRequest(); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $this->getCategoryTable()->save($category); return $this->redirect()->toRoute('category'); } } return array('id' => $id, 'form' => $form); }