예제 #1
0
 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 addAction()
 {
     $form = new CategoryForm();
     $request = $this->getRequest();
     if ($request->isPost()) {
         $category = new Category();
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $category->exchangeArray($form->getData());
             $this->getCategoryTable()->save($category);
             // Redirect to list of category
             return $this->redirect()->toRoute('category');
         }
     }
     return array('form' => $form);
 }