Php editAction zend框架2理论2

Php editAction zend框架2理论2,php,doctrine-orm,zend-framework2,Php,Doctrine Orm,Zend Framework2,我正在使用zendframework 2和doctrine 2。我想编辑实体OptionVehicleue的值。我从以下方面获得了该技术: 这是我的实体: class Optionsvehicule { protected $inputFilter; /** * @var integer * * @ORM\Column(name="idOptVeh", type="integer", nullable=false) * @ORM\Id

我正在使用zendframework 2和doctrine 2。我想编辑实体OptionVehicleue的值。我从以下方面获得了该技术: 这是我的实体:

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总是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'
                                )
                        )
                )

        )
);

假设
/vehicleus
是您所指的路线,您需要将
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。在param getter前面投射(int)也可能是一个好主意


我希望这能让您更清楚地了解路由。

尝试在控制器$this->getEvent()->getRouteMatch()->getParam('id')中使用路由匹配;还可以尝试转储var_dump($v->getIdoptveh());在您的.phtml中,同样的问题:/but$v->getIdoptveh()返回选项的所有id!!请告诉我们您的路线。您是否在此处添加了任何约束?我已通过添加我的module.config.phpthank you:)编辑了我的问题。。我在module.config.php中添加了这一行:“id'=>'[0-9]+',但是我仍然有同样的问题:当抛出错误时,浏览器中的url是什么样子的?您还必须在配置的路由部分添加[/:id]。不会引发错误,但id始终为空!!所以我回到索引:/ooh谢谢你:D它现在可以工作了,我已经忘记了[/:id]。
http://domain.bla/vehicules/edit/12