2016-08-03 106 views
1

有人能向我解釋如何解決這個錯誤「沒有對象管理器設置」沒有對象管理器設置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,我需要從數據庫中選擇類別並將它們顯示在選擇框中。

如果您需要更多的代碼或解釋,請問我。

回答

0

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

相關問題