【问题标题】:No object manager was set zf2 fieldset没有设置对象管理器 zf2 fieldset
【发布时间】:2016-08-03 21:31:53
【问题描述】:

谁能告诉我如何解决这个错误“没有设置对象管理器”

这是字段集:

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

没有设置对象管理器

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

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

【问题讨论】:

    标签: forms doctrine-orm zend-framework2 fieldset


    【解决方案1】:

    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 ;
    

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

    您的表单和他的工厂与字段集完全不同,因此它不能以这种方式为您构造注入。

    【讨论】:

    • 首先,我想对迟到的回复表示抱歉,谢谢@Hooli。你的答案是正确的!我在表单和字段集中使用初始化程序而不是构造函数。我使用接口 ObjectManagerAwareInterface 而不是 trait。我按照这里的解释github.com/doctrine/DoctrineModule/issues/175
    【解决方案2】:

    是否有任何使用 Laminas 和 Doctrine hydrator 进行表格水合或提取和表格绑定的工作示例?我一直在到处搜索,Doctrine hydrator 文档并没有真正解释这如何依赖于一对一的关系,比如用户和地址。

    【讨论】:

    • 这似乎是一个问题,而不是一个答案。如果是这种情况,请创建一个单独的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2013-03-20
    相关资源
    最近更新 更多