Php addAction、editAction zend framework2 doctrine2

Php addAction、editAction zend framework2 doctrine2,php,zend-framework,doctrine-orm,zend-framework2,doctrine-1.2,Php,Zend Framework,Doctrine Orm,Zend Framework2,Doctrine 1.2,我正在使用zendframework 2和doctrine 2。我的addAction不起作用我没有任何错误,但是当我验证我的表单时,我的数据库中没有创建任何行!! 我想我在填充外键方面有问题! 这是我的表格: <?php // filename : module/Users/src/Users/Form/addForm.php namespace Vehicules\Form; use Zend\Form\Form; use DoctrineModule\Persistence\Obje

我正在使用zendframework 2和doctrine 2。我的addAction不起作用我没有任何错误,但是当我验证我的表单时,我的数据库中没有创建任何行!! 我想我在填充外键方面有问题! 这是我的表格:

<?php
// filename : module/Users/src/Users/Form/addForm.php
namespace Vehicules\Form;
use Zend\Form\Form;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
class VehiculeForm extends form implements ObjectManagerAwareInterface
{ 
protected $objectManager;
public function setObjectManager(ObjectManager $objectManager)
{
    $this->objectManager = $objectManager;
}

public function getObjectManager()
{
    return $this->objectManager;
}
    //public function init()
public function __construct(ObjectManager $objectManager)
{
    parent::__construct('add');

    $this->objectManager = $objectManager;
    $this->init();
}
    public function init(){
        $this->setAttribute('method', 'post');
        $this->setAttribute('enctype','multipart/formdata');
        $this->add(array(
                'name' => 'matricule',
                'attributes' => array(
                        'type' => 'text',
                        'required' => true
                ),
                'options' => array(
                        'label' => 'Matricule',
                ),
        ));


        $this->add(array(
                 'type' => 'Zend\Form\Element\Select',
                 'name' => 'carburant',
                 'options' => array(
                         'label' => 'Carburant',
                         'value_options' => array(

                                 '0' => 'Essence',
                                 '1' => 'Gasoil',
                                 '2' => 'Hybride',
         ),
         )
         ));
        $this->add(array(
            'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
            'name' => 'option',
            'options' => array(
                'label' => 'Options Véhicule',
                'object_manager' => $this->getObjectManager(),
                'target_class'   => 'Vehicules\Entity\optionsvehicule',
                    'property'   => 'libellee',
                  )));   
        $this->add(array(
                'type' => 'DoctrineModule\Form\Element\ObjectSelect',
                'name' => 'categorie',
                'options' => array(
                        'label' => 'categorie',
                        'object_manager' => $this->getObjectManager(),
                'target_class'   => 'Vehicules\Entity\categorie',
                    'property'   => 'idcat',

                )
        ));
            $this->add(array(
                'type' => 'DoctrineModule\Form\Element\ObjectSelect',
                'name' => 'modele',
                'options' => array(
                        'label' => 'Modèle',
                        'object_manager' => $this->getObjectManager(),
                'target_class'   => 'Vehicules\Entity\modele',
                    'property'   => 'nom',
                )
        ));
        /*$this->add(array(
                'type' => 'DoctrineModule\Form\Element\ObjectSelect',
                'name' => 'modele',
                'options' => array(
                        'label' => 'Modèle',
                        'object_manager' => $this->getObjectManager(),
                        'target_class'   => 'Vehicules\Entity\modele',
                        'property'   => 'nom',
                        'is_method'      => true,
                        'find_method'    => array(
                                'name'   => 'findBy',
                                'params' => array(
                                        'criteria' => array('active' => 1),

                                        // Use key 'orderBy' if using ORM
                                        'orderBy'  => array('lastname' => 'ASC'),

                                        // Use key 'sort' if using ODM
                                        'sort'  => array('lastname' => 'ASC')
                                ),
                        ),
                ),
        ));*/
        $this->add(array(
                'type' => 'DoctrineModule\Form\Element\ObjectSelect',
                'name' => 'marque',
                'options' => array(
                        'label' => 'Marque',
                        'object_manager' => $this->getObjectManager(),
                        'target_class'   => 'Vehicules\Entity\marque',
                        'property'   => 'nom',
                )
        ));

        $this->add(array(
                'name' => 'dateMiseCirculation',
                'attributes' => array(
                        'type' => 'Zend\Form\Element\Date',
                ),
                'options' => array(
                        'label' => 'Date de Mise en Circulation',
                ),
        ));
        $this->add(array(
                'name' => 'numChasis',
                'attributes' => array(
                        'type' => 'text',
                ),
                'options' => array(
                        'label' => 'Numero de Chasis',
                ),
        ));
        $this->add(array(
                'name' => "Prix d'achat",
                'attributes' => array(
                        'type' => 'int',
                ),
                'options' => array(
                        'label' => "Prix d'achat",
                ),
        ));

        $this->add(array(
                'name' => 'concessionnaire',
                'attributes' => array(
                        'type' => 'text',
                ),
                'options' => array(
                        'label' => 'concessionnaire',
                ),
        ));


        $this->add(array(
                'name' => 'souslocation',
                'attributes' => array(
                        'type' => 'string',
                ),
                'options' => array(
                        'label' => 'Sous-location',
                ),
        )); 
        $this->add(array(
                'name' => 'remarque',
                'attributes' => array(
                        'type' => 'text',
                ),
                'options' => array(
                        'label' => 'remarque',
                ),
        )); 
        $this->add(array(
                'name' => 'puisfiscal',
                'attributes' => array(
                        'type' => 'int',
                ),
                'options' => array(
                        'label' => "puissance fiscale",
                ),
        ));
        $this->add(array(
                'type' => 'Zend\Form\Element\Select',
                'name' => 'nbreport',
                'options' => array(
                        'label' => 'Nombre de portes',
                        'value_options' => array(

                                '0' => '4',
                                '1' => '2',
                                '2' => '5',
                                '3' => '6',
                                '4' => '7',
                                '5' => '7',
                        ),
                )
        ));


        $this->add(array(
                'name' => 'dernierKm',
                'attributes' => array(
                        'type' => 'text',
                ),
                'options' => array(
                        'label' => 'Dernier  kilométrage',
                ),
        ));

        $this->add(array(
                'name' => 'submit',
                'attributes' => array(
                        'type' => 'submit',
                        'value' => 'Valider'
                ),
                ));
    }}

但是id不起作用:///p>你的问题是形式/实体水合作用。由于
Zend\Form
已经负责从表单中获取数据,因此不需要在这里重新设计轮子

当前您的表单使用默认值,因此当表单被验证并获取数据时(使用
$form->getData()
),您将获得一个
数组。您需要的是一个已填充的
车辆
实体

这是一个专门为教义而设计的程序。如果你将它作为水合器连接到你的身体上,它将返回水合实体

要解决此问题,您需要停止使用
new

$form = new VehiculeForm($_objectManager);
而是通过服务管理器创建它

$form = $this->getServiceLocator()->get('MyModule\Form\VehiculeForm');
现在从服务工厂创建表单;允许注入表单依赖项,包括生成器

MyModule/Factory/Form/vehicleuleformfactory.php

namespace MyModule\Factory\Form;

use MyModule\Entity;
use MyModule\Form;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;
use DoctrineModule\Stdlib\Hydrator;

class VehiculeFormFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $sl)
    {
        $objectManager = $sl->get('Doctrine\ORM\EntityManager');

        $form = new Form\VehiculeForm($objectManager);
        $vehicule = new Entity\Vehicule();

        // create the hydrator; this could also be done via the
        // hydrator manager but create here for the example
        $hydrator = new DoctrineObject($objectManager);

        $form->setHydrator($hydrator);
        $form->setObject($vehicule);

        // We can also take care of the input filter here too!
        // meaning less code in the controller and only
        // one place to modify should it change
        $form->setInputFilter($Vehicule->getInputFilter());

        return $form;
    }
}
public function getFormElementConfig()
{
    return array(
        'factories' => array(
            'MyModule\Form\VehiculeForm' => 'MyModule\Factory\Form\VehiculeForm' // path to our new factory
        ),
    );
}
//...
protected function getVehiculeForm()
{
    return $this->getServiceLocator()->get('MyModule\Form\VehiculeForm');
}

public function addAction()
{   
    $request = $this->getRequest();
    $form    = $this->getVehiculeForm();

    if ($request->isPost()) {

        $form->setData($request->getPost());

        if ($form->isValid()) {
            $vehicule = $form->getData(); // returns the entity!

            if ($vehicule instanceof Vehicule) {
                /// Fully hydrated entity!
                var_dump($vehicule);
            }
        } 
    }
    //...
}
//...
Module.php

namespace MyModule\Factory\Form;

use MyModule\Entity;
use MyModule\Form;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;
use DoctrineModule\Stdlib\Hydrator;

class VehiculeFormFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $sl)
    {
        $objectManager = $sl->get('Doctrine\ORM\EntityManager');

        $form = new Form\VehiculeForm($objectManager);
        $vehicule = new Entity\Vehicule();

        // create the hydrator; this could also be done via the
        // hydrator manager but create here for the example
        $hydrator = new DoctrineObject($objectManager);

        $form->setHydrator($hydrator);
        $form->setObject($vehicule);

        // We can also take care of the input filter here too!
        // meaning less code in the controller and only
        // one place to modify should it change
        $form->setInputFilter($Vehicule->getInputFilter());

        return $form;
    }
}
public function getFormElementConfig()
{
    return array(
        'factories' => array(
            'MyModule\Form\VehiculeForm' => 'MyModule\Factory\Form\VehiculeForm' // path to our new factory
        ),
    );
}
//...
protected function getVehiculeForm()
{
    return $this->getServiceLocator()->get('MyModule\Form\VehiculeForm');
}

public function addAction()
{   
    $request = $this->getRequest();
    $form    = $this->getVehiculeForm();

    if ($request->isPost()) {

        $form->setData($request->getPost());

        if ($form->isValid()) {
            $vehicule = $form->getData(); // returns the entity!

            if ($vehicule instanceof Vehicule) {
                /// Fully hydrated entity!
                var_dump($vehicule);
            }
        } 
    }
    //...
}
//...
您现在可以摆脱
populate()
方法并分配控制器代码

FooController.php

namespace MyModule\Factory\Form;

use MyModule\Entity;
use MyModule\Form;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;
use DoctrineModule\Stdlib\Hydrator;

class VehiculeFormFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $sl)
    {
        $objectManager = $sl->get('Doctrine\ORM\EntityManager');

        $form = new Form\VehiculeForm($objectManager);
        $vehicule = new Entity\Vehicule();

        // create the hydrator; this could also be done via the
        // hydrator manager but create here for the example
        $hydrator = new DoctrineObject($objectManager);

        $form->setHydrator($hydrator);
        $form->setObject($vehicule);

        // We can also take care of the input filter here too!
        // meaning less code in the controller and only
        // one place to modify should it change
        $form->setInputFilter($Vehicule->getInputFilter());

        return $form;
    }
}
public function getFormElementConfig()
{
    return array(
        'factories' => array(
            'MyModule\Form\VehiculeForm' => 'MyModule\Factory\Form\VehiculeForm' // path to our new factory
        ),
    );
}
//...
protected function getVehiculeForm()
{
    return $this->getServiceLocator()->get('MyModule\Form\VehiculeForm');
}

public function addAction()
{   
    $request = $this->getRequest();
    $form    = $this->getVehiculeForm();

    if ($request->isPost()) {

        $form->setData($request->getPost());

        if ($form->isValid()) {
            $vehicule = $form->getData(); // returns the entity!

            if ($vehicule instanceof Vehicule) {
                /// Fully hydrated entity!
                var_dump($vehicule);
            }
        } 
    }
    //...
}
//...

我投票结束了你的问题;因为这是毫无疑问的。你需要清楚地解释问题以及你试图完成的目标,而不是“它不起作用”对不起:/我会编辑它,但请不要关闭它,我真的需要你的帮助,没关系;我已经更详细地阅读了您的代码,并且认为我理解您正在尝试做什么。不是每个人都会在一个问题上花费那么多时间,所以我的上述评论是为了您的利益。好的,非常感谢:)我可以使用名称空间MyModule\Form;不创建新文件夹工厂??如果不是,那有什么重要的?我已经完成了所有这些操作,但出现了一个错误:Zend\ServiceManager\ServiceManager::get无法获取或创建Vehicles\Form\VehicleFormPublic函数填充($data){$this->setMatricule($data['matricule'])的实例…例如,这一行填充函数让理论影响形式的矩阵到我的实体的矩阵,但没有这个函数,水合器怎么知道!!在我们的例子中,我们有相同的名称“矩阵”,但有时我们没有相同的名称!!