Example #1
0
 /**
  * Create service
  *
  * @param ServiceLocatorInterface $serviceLocator
  * @return mixed
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     /**
      * @var \Doctrine\ORM\EntityManager $entityManager
      */
     $entityManager = $serviceLocator->get('Doctrine\\ORM\\EntityManager');
     $formManager = $serviceLocator->get('FormElementManager');
     /**
      * @var PostFieldset $fieldset
      */
     $fieldset = $formManager->get('Post\\Form\\PostFieldset');
     $form = new PostForm();
     $hydrator = new DoctrineHydrator($entityManager, '\\Post\\Entity\\Post');
     $fieldset->setUseAsBaseFieldset(true)->setHydrator($hydrator)->setObject(new Post());
     $form->add($fieldset)->setInputFilter(new InputFilter())->setHydrator($hydrator);
     return $form;
 }
 public function editAction()
 {
     $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         return $this->redirect()->toRoute('post', array('action' => 'add'));
     }
     $post = $this->getEntityManager()->find('Post\\Entity\\Post', $id);
     if (!$post) {
         return $this->redirect()->toRoute('post', array('action' => 'index'));
     }
     $form = new PostForm();
     $form->bind($post);
     $form->get('submit')->setAttribute('value', 'Edit');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setInputFilter($post->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $this->getEntityManager()->flush();
             return $this->redirect()->toRoute('post');
         }
     }
     return array('id' => $id, 'form' => $form);
 }