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

时间:2014-05-17 22:01:47

标签: php doctrine-orm zend-framework2

我正在使用zendframework 2和doctrine 2.我想从我的数据库中的值填充MultiCheckbox的值。 我从https://github.com/doctrine/DoctrineModule/blob/master/docs/form-element.md

获得了这项技术
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)
{
    parent::__construct('add');
    $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'
            ,   )));

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

2 个答案:

答案 0 :(得分:1)

我试过并发现了类似的错误。经过一些搜索后,我发现了https://github.com/doctrine/DoctrineModule/issues/175上发布的解决方案。哪个有用。

对于实现,您需要进行类似的更改

在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');

答案 1 :(得分:0)

$objectManager属性未定义。

这是因为您在设置变量之前立即在$this->getObjectManager()内调用__construct()方法。

表单取决于对象管理器;所以你可以将它添加为构造函数参数,以确保在使用类之前设置它。

此外,构造函数应该只用于设置对象的初始属性和状态,使用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;
                },
            ), 
        );
    }
}