editAction zend framework2 doctrine2


editAction zend framework2 doctrine2

我使用zendframework 2和doctrine 2。我想编辑我的实体选项车辆的值。我得到了技术: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:

的页面id . php。
 <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总是合法的0 !!

这是我的模块。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'
                                )
                        )
                )
        )
);

假设/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(
      ),
   ),
),

如果路由匹配和约束是正确的,那么浏览器中的url应该变成如下所示:

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

现在,一旦你用$this->getEvent()->getRouteMatch()->getParam('id');检查参数,它应该显示12,如果一切都配置可能。在参数getter的前面强制转换(int)可能也是一个好主意。