Php zendframework 2表单从数据库填充MultiCheckbox值

Php zendframework 2表单从数据库填充MultiCheckbox值,php,doctrine-orm,zend-framework2,Php,Doctrine Orm,Zend Framework2,我正在使用zendframework 2和doctrine 2。我想从数据库中的值填充MultiCheckbox的值。 我从以下方面获得了技术: 我收到的错误消息: 未设置对象管理器。未定义$objectManager属性 这是因为在设置变量之前,在\uu construct()中立即调用$This->getObjectManager()方法 形式取决于对象管理器;因此,您可以将其作为构造函数参数添加,以确保在使用类之前设置它 此外,构造函数实际上应该只用于设置对象的初始属性和状态,使用init

我正在使用zendframework 2和doctrine 2。我想从数据库中的值填充MultiCheckbox的值。 我从以下方面获得了技术:

我收到的错误消息:
未设置对象管理器。

未定义
$objectManager
属性

这是因为在设置变量之前,在
\uu construct()
中立即调用
$This->getObjectManager()
方法

形式取决于对象管理器;因此,您可以将其作为构造函数参数添加,以确保在使用类之前设置它

此外,构造函数实际上应该只用于设置对象的初始属性和状态,使用
init()
修改表单元素

class addForm extends Form
{
    protected $objectManager;

    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('add-form');

        $this->objectManager = $objectManager;
    }

    // The form element manager will call `init()` 
    // on the form so we can add the elements in this method
    public function init() {
        //....
        $this->setAttribute('method', 'post');
        $this->setAttribute('enctype','multipart/formdata');

        // $this->add(....
        // more elements added here
    }

}
最后一件事是注册一家实际进行注射的工厂

class Module {

    public function getFormElementConfig() {
        return array(
            'factories' => array(
                'ModuleName\Form\FooForm' => function($formElementManager) {
                    $serviceManager = $formElementManager->getServiceLocator();
                    $objectManager  = $serviceManager->get('ObjectManager'); 

                    $form = new Form\FooForm($objectManager); 

                    return $form;
                },
            ), 
        );
    }
}

我试过了,发现了类似的错误。经过一番搜索,我在网上找到了答案。这很有效

对于实现,您需要做一些这样的更改

在Module.php中添加方法getFormElementConfig:

public function getFormElementConfig()
{
    return array(
        'invokables' => array(
            'addForm' => 'Users\Form\addForm',
        ),
        'initializers' => array(
            'ObjectManagerInitializer' => function ($element, $formElements) {
                if ($element instanceof ObjectManagerAwareInterface) {
                    $services      = $formElements->getServiceLocator();
                    $entityManager = $services->get('Doctrine\ORM\EntityManager');

                    $element->setObjectManager($entityManager);
                }
            },
        ),
    );
}
在表单类addForm.php中,将构造函数替换为init方法:

namespace Users\Form;
use Zend\Form\Form;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
class addForm extends form implements ObjectManagerAwareInterface
{
    protected $objectManager;
    public function setObjectManager(ObjectManager $objectManager)
    {
        $this->objectManager = $objectManager;
    }

    public function getObjectManager()
    {
        return $this->objectManager;
    }

    //public function __construct($name = null)
    public function init()
    {
        $this->setAttribute('method', 'post');
        $this->setAttribute('enctype','multipart/formdata');
        $this->add(array(
            'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
            'name' => 'option',
            'options' => array(
                'label' => 'Options Véhicule',
                'object_manager' => $this->getObjectManager(),
                'target_class'   => 'Users\Entity\optionsvehicule',
                'property'       => 'property'
            ,   )));
在控制器类中,通过服务定位器调用表单obejct:

//$form = new addForm();
$forms = $this->getServiceLocator()->get('FormElementManager');
$form = $forms->get('addForm');

它是由依赖项注入设置的吗?@alex它怎么可能是
\uu构造
调用方法
$this->getObjectManager()
,这是以前无法设置的。在构造函数中没有看到它…这就是为什么我真的很困惑的原因,我以为所有表单的元素都添加到表单中了,因为构造函数带有“$this->add(…”。我将发布我所有的表单来澄清这一点谢谢你,我做了你所说的一切,但我得到了一个错误:在对象“vehicles\Entity\optionVehicle”中找不到属性“Property”!!谢谢,请说明我们为什么要添加此行;'property'=>'property'这有一个错误:在对象“vehicles\Entity\options车辆”中找不到属性“property”实际上这应该是
property'=>'propertyName'
其中propertyName是每个复选框显示的字段。因此它可能类似于
property'=>'username'
请以相同的形式添加ObjectSelect,但我遇到了以下错误:Zend\form\FormElementManager::get无法获取或创建insZend\Form\Element\ObjectSelect的实例!!
//$form = new addForm();
$forms = $this->getServiceLocator()->get('FormElementManager');
$form = $forms->get('addForm');