没有对象管理器设置ZF2字段集

问题描述:

有人能向我解释如何解决这个错误“没有对象管理器设置”没有对象管理器设置ZF2字段集

这里是字段集:

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 \供应商\主义\主义模块的\ src \ DoctrineModule \表格\元素\ Proxy.php:535

无对象管理器已设置

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

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

Fieldset对象可以用Hydrator为您的实体提供水合物。

这里是一个字段 https://github.com/Grafikart/BlogMVC/blob/master/ZendFramework2/module/Blog/src/Blog/Form/Fieldset/CommentFieldset.php

正如你所看到的字段集可以通过“awareInterface”命名ObjectManagerAwareInterface有对象管理的一个完整的例子,

use DoctrineModule\Persistence\ObjectManagerAwareInterface; 

与性状:use ProvidesObjectManager;

use DoctrineModule\Persistence\ProvidesObjectManager as ProvidesObjectManager ; 

你错过了那些在你的字段集中,这应该是cor纠正你的问题。

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

+0

首先,我想对最近的回复表示歉意,并感谢@Hooli。你的回答是正确的!我在表单和字段集中使用了Initializers而不是构造函数。我使用INTERFACE ObjectManagerAwareInterface而不是特质。我遵循https://github.com/doctrine/DoctrineModule/issues/175中的解释 – Sezgin