Skip to content

Instantly share code, notes, and snippets.

@manuakasam
Created December 30, 2012 20:21
Show Gist options
  • Select an option

  • Save manuakasam/4414902 to your computer and use it in GitHub Desktop.

Select an option

Save manuakasam/4414902 to your computer and use it in GitHub Desktop.
public function editAction()
{
$vm = new ViewModel();
$vm->setTemplate('haushaltportal/ergebnishaushalt/edit.phtml');
/**
* Service-Manager ansprechen um Objekte zu erhalten
*
* @var $form \Haushaltportal\Form\ErgebnishaushaltProduktForm
* @var $entityService \Haushaltportal\Service\ErgebnishaushaltProduktService
*/
$form = $this->getServiceLocator()->get('ergebnishaushalt-form-produkt');
$entityService = $this->getServiceLocator()->get('ergebnishaushalt-service-produkt');
$request = $this->getRequest();
$id = $this->params('id');
$entity = $entityService->find($id);
$form->bind($entity);
//\Zend\Debug\Debug::dump($form->getObject());
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
try {
$entityService->persist($entity);
$vm->setVariable('message', 'Erfolgreich bearbeitet!');
} catch (\Exception $e) {
$form->setMessages(array(
'submit' => array('Datenbankfehler. Folgende Nachricht wurde ausgegeben: ' . $e->getMessage())
));
}
}
}
return $vm->setVariable('form', $form);
}
public function __construct($name, $options = array())
{
parent::__construct($name, $options);
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden'
)
));
$this->add(new \Zend\Form\Element\Csrf('awesomesecurity'));
$this->add(array(
'name' => 'produktBereich',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'attributes' => array(
'required' => true
),
'options' => array(
'label' => 'Produktbereich',
'empty_option' => '--- Produktbereich auswählen ---',
'object_manager' => $this->getEntityManager(),
'target_class' => 'Haushaltportal\Entity\Produktbereich',
'label_generator' => function ($targetEntity) {
return $targetEntity->getNummer() . ' - ' . $targetEntity->getTitelOriginal();
}
)
));
// more Form Stuff
@manuakasam

Copy link
Copy Markdown
Author
class ErgebnishaushaltProduktFormFactory implements FactoryInterface
{
    /**
     * Create service
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @return \Haushaltportal\Service\ErgebnishaushaltProduktService
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $em = $serviceLocator->get('Doctrine\ORM\EntityManager');

        $form = new ErgebnishaushaltProduktForm('ergebnisform', array(
            'entity_manager' => $em
        ));

        $form->setHydrator(new DoctrineEntity($em))
             ->setObject(new ErgebnishaushaltProdukt())
             ->setInputFilter(new ErgebnishaushaltProduktFilter())
             ->setAttribute('method', 'post');

        return $form;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment