没有设置对象管理器 zf2 fieldset

No object manager was set zf2 fieldset

有人可以向我解释如何解决“未设置对象管理器”这个错误吗

这是字段集:

namespace Trunk\Form;

use Trunk\Entity\Category;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;

class CategoryFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct($objectManager)
    {
        parent::__construct('category');

        $this->setHydrator(new DoctrineHydrator($objectManager, 'Trunk\Entity\Category'));

        $this->add(array(
            'type' => 'DoctrineORMModule\Form\Element\DoctrineEntity',
            'name' => 'title',
            'object_manager' => $objectManager,
            'target_class'   => 'Trunk\Entity\Category',
            'property'       => 'title',
            'is_method'      => false,
            'find_method'    => array(
                'name'   => 'findBy',
                'params' => array(
                    'criteria' => array('parentid' => 0),
                    'orderBy'  => array('title' => 'ASC'),
                ),
            )
       ));
    }
}

错误信息如下:

F:\xampp\htdocs\travelltheworld\vendor\doctrine\doctrine-module\src\DoctrineModule\Form\Element\Proxy.php:535

No object manager was set

我已经将工厂中的实体管理器注入到我的名为 ProductForm 的表单中。在该表单中,我有一个名为 ProductFieldset 的基本字段集,在 ProductFieldset 中,我在需要 select 数据库中的类别并将它们显示在 select 框中的地方插入了 CategoryFieldset。

如果您需要更多代码或解释,请问我。

Fieldset 对象可以使用 Hydrator 对您的实体进行水合。

这是一个完整的字段集示例 https://github.com/Grafikart/BlogMVC/blob/master/ZendFramework2/module/Blog/src/Blog/Form/Fieldset/CommentFieldset.php

如您所见,字段集可以通过名为 ObjectManagerAwareInterface

的 "awareInterface" 拥有对象管理器
use DoctrineModule\Persistence\ObjectManagerAwareInterface;

和特征:use ProvidesObjectManager;

use DoctrineModule\Persistence\ProvidesObjectManager as ProvidesObjectManager ;

您在字段集中遗漏了那些,这应该可以解决您的问题。

你的form,他的factory跟fieldset完全不一样,你构造注入是不行的。

是否有任何使用 Laminas 和 Doctrine 水化器进行形式水合或提取和形式绑定的工作示例?我一直在到处搜索,Doctrine hydrator 文档并没有真正解释这在一对一关系(如用户和地址)上是如何下降的。