editAction zend framework2 doctrine2

时间:2014-05-27 22:47:54

标签: php doctrine-orm zend-framework2

我正在使用zendframework 2和doctrine 2.我想编辑我的Entity optionvehicule的值。我从以下方法获得了技术:http://stevenwilliamalexander.wordpress.com/2013/08/28/zf2-with-doctrine-2-orm/ 这是我的实体:

class Optionsvehicule
{    protected  $inputFilter;
    /**
     * @var integer
     *
     * @ORM\Column(name="idOptVeh", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idoptveh;

    /**
     * @var string
     *
     * @ORM\Column(name="libellee", type="string", length=255, nullable=true)
     */
    private $libellee;

    /**
     * @var string
     *
     * @ORM\Column(name="remarques", type="text", nullable=true)
     */
    private $remarques;


    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Vehicules\Entity\Vehicule", mappedBy="idoptveh")
     */
    private $idveh;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->idveh = new \Doctrine\Common\Collections\ArrayCollection();
    }


    /**
     * Get idoptveh
     *
     * @return integer 
     */
    public function getIdoptveh()
    {
        return $this->idoptveh;
    }

    /**
     * Set libellee
     *
     * @param string $libellee
     * @return Optionsvehicule
     */
    public function setLibellee($libellee)
    {
        $this->libellee = $libellee;

        return $this;
    }


    /**
     * Get libellee
     *
     * @return string 
     */
    public function getLibellee()
    {
        return $this->libellee;
    }

    /**
     * Set remarques
     *
     * @param string $remarques
     * @return Optionsvehicule
     */
    public function setRemarques($remarques)
    {
        $this->remarques = $remarques;

        return $this;
    }

    /**
     * Get remarques
     *
     * @return string 
     */
    public function getRemarques()
    {
        return $this->remarques;
    }

    /**
     * Add idveh
     *
     * @param \Vehicules\Entity\Vehicule $idveh
     * @return Optionsvehicule
     */
    public function addIdveh(\Vehicules\Entity\Vehicule $idveh)
    {
        $this->idveh[] = $idveh;

        return $this;
    }

    /**
     * Remove idveh
     *
     * @param \Vehicules\Entity\Vehicule $idveh
     */
    public function removeIdveh(\Vehicules\Entity\Vehicule $idveh)
    {
        $this->idveh->removeElement($idveh);
    }

    /**
     * Get idveh
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getIdveh()
    {
        return $this->idveh;
    }
    public function populate($data) {
        $this->setLibellee($data['libellee']) ;
        $this->setRemarques($data['remarque']);

    }
    public function setInputFilter(InputFilterInterface $inputFilter)
    {
        throw new \Exception("Not used");
    }

    public function getInputFilter()
    {
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();
            $factory = new InputFactory();
            $inputFilter->add($factory->createInput(array(
                    'name' => 'libellee',
                    'required' => true,
                    'filters' => array(
                            array('name' => 'StripTags'),
                            array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                            array(
                                    'name' => 'StringLength',
                                    'options' => array(
                                            'encoding' => 'UTF-8',
                                            'min' => 1,
                                            'max' => 3,
                                    ),
                            ),
                    ),
            )));

            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }
}

这是我的控制器的编辑:

 public function editAction()
     {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('vehicules/default', array('controller'=>'option','action'=>'index'));
        }
        $a = $this->getObjectManager()->find('Vehicules\Entity\Optionsvehicule', $id);

        $form = new optionForm();
        $form->setBindOnValidate(false);
        $form->bind($a);
        $form->get('submit')->setAttribute('label', 'Edit');
        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->post());
            if ($form->isValid()) {
                $form->bindValues();
                $this->getObjectManager()->flush();

                // Redirect to list of options

                return $this->redirect()->toRoute('vehicules/default', array('controller'=>'option','action'=>'ind'));
            }
        }

        return array(
                'id' => $id,
                'form' => $form,
        );

     }

这是调用editAction:

的页面ind.phtml
 <a href="<?php echo $this->url('vehicules/default', array('controller'=>'option','action'=>'edit','id' => $v->getIdoptveh()));?>">Edit</a> 

当我尝试编辑时,我会回到索引页面

 $id = (int) $this->params()->fromRoute('id', 0);
            if (!$id) {
                return $this->redirect()->toRoute('vehicules/default', array('controller'=>'option','action'=>'index'));

所以id始终是egal 0 !!

这是我的module.config.php:

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Vehicules\Controller\Vehicules' => 'Vehicules\Controller\VehiculesController',
                'Vehicules\Controller\option' => 'Vehicules\Controller\optionController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'vehicules' => array(
                'type'    => 'Literal',
                'options' => array(
                    // Change this to something specific to your module
                    'route'    => '/vehicules',
                    'defaults' => array(
                        // Change this value to reflect the namespace in which
                        // the controllers for your module are found
                        '__NAMESPACE__' => 'Vehicules\Controller',
                        'controller'    => 'Vehicules',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    // This route is a sane default when developing a module;
                    // as you solidify the routes for your module, however,
                    // you may want to remove it and replace it with more
                    // specific routes.
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'Vehicules' => __DIR__ . '/../view',
        ),
    ),
        'doctrine' => array(
                'driver' => array(
                        'Vehicules_driver' => array(
                                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                                'cache' => 'array',
                                'paths' => array(__DIR__ . '/../src/Vehicules/Entity')
                        ),
                        'orm_default' => array(
                                'drivers' => array(
                                        'Vehicules\Entity' => 'Vehicules_driver'
                                )
                        )
                )

        )
);

1 个答案:

答案 0 :(得分:0)

假设/vehicules是您所指的路线,则需要在路线配置中添加id作为参数。

'default' => array(
   'type'    => 'Segment',
   'options' => array(
      'route'    => '/[:controller[/:action]][/:id]',
      'constraints' => array(
         'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
         'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
         'id'         => '[0-9]+',
      ),
      'defaults' => array(
      ),
   ),
),

如果带有约束的路径匹配正确,浏览器中的网址应更改为此类:

http://domain.bla/vehicules/edit/12

现在,一旦您检查了$this->getEvent()->getRouteMatch()->getParam('id');的参数,如果所有内容都已配置,则应显示12。在param getter的前面施放(int)也可能是一个好主意。

我希望你的路由能够更加清晰。