Doctrine表单元素+ zf3错误messaje“没有设置对象管理器”

时间:2017-09-28 17:48:49

标签: php forms doctrine-orm zend-framework3

我使用Doctrine在我的表单中创建一个DoctrineModule \ Form \ Element \ ObjectSelect。但是向我展示了这些错误:“没有设置对象管理器”。我的基础是Doctrine Module的指南。我有时间搜索,但找不到什么是错的。代码:

表格:

<?php
//.....
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Zend\Form\Form;

class SubRubroForm extends Form implements ObjectManagerAwareInterface
{
    private $value_submit;
    private $objectManager;

    public function __construct($value_submit)
    {
        $this->value_submit=$value_submit;
        // Define form name
        parent::__construct('SubRubro-form');

        // Set POST method for this form
        $this->setAttribute('method', 'post');
        $this->addElements();
        $this->addInputFilter();
        $this->init();            
    }

    public function init()
    {
        $this->add([
            'type' => 'DoctrineModule\Form\Element\ObjectSelect',
            'name' => 'rubro',
            'options' => [
                'object_manager' => $this->getObjectManager(),
                'target_class'   => 'Rubros\Entity\Rubro',
                'property'       => 'nombre',
            ],
        ]);
    }
// ... add others elements addElements(){} ....
// ... inputfilters ....
// ... set and get ObjectManager() interface methods...

}

1 个答案:

答案 0 :(得分:0)

从父类添加init()方法,ObjectManager加载的元素很好。

类SubRubroForm extends Form实现ObjectManagerAwareInterface {private $ value_submit; private $ objectManager;

public function __construct($value_submit)
{
    $this->value_submit=$value_submit;
    // Define form name
    parent::__construct('SubRubro-form');

    // Set POST method for this form
    $this->setAttribute('method', 'post');
    $this->addElements();
    $this->addInputFilter();
    $this->init();            
}

public function init()
{
    $this->add([
        'type' => 'DoctrineModule\Form\Element\ObjectSelect',
        'name' => 'rubro',
        'options' => [
            'object_manager' => $this->getObjectManager(),
            'target_class'   => 'Rubros\Entity\Rubro',
            'property'       => 'nombre',
        ],
    ]);
    parent::init();
}
// ... add others elements addElements(){} ....
// ... inputfilters ....
// ... set and get ObjectManager() interface methods...