DoctrineModule \ Form \ Element \ ObjectSelect未设置对象管理器

时间:2017-07-03 15:18:15

标签: doctrine-orm zend-framework3

我试图添加' DoctrineModule \ Form \ Element \ ObjectSelect'到我的表单,但它一直给我这个错误:没有设置对象管理器

这是我表单的代码:

use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Retrieve\Entity\Autos;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;

class AddForm extends Form
{ 
  /**
 * Entity manager.
 * @var Doctrine\ORM\EntityManager 
 */
private $entityManager;

protected $objectManager;

public function __construct()
{
    parent::__construct('addforms');

    $this->setAttribute('method', 'post');

    $this->addElements();
    $this->addInputFilter();  

}

protected function addElements() 
{           
    // Add "title" field
    $this->add([        
        'type'  => 'text',
        'name' => 'title',
        'attributes' => [
            'id' => 'title'
        ],
        'options' => [
            'label' => 'Title',
        ],
    ]);

 $this->add([
        'type' => 'DoctrineModule\Form\Element\ObjectSelect',
        'name' => 'country',
        'options' => [
            'label' => 'Country',
            'object_manager' =>  $this->getObjectManager(),
            'target_class'   => 'BusinessGhana\Entity\Country',
            'property'       => 'country',
            'display_empty_item' => true,
            'empty_item_label'   => '--select Country--',                
        ],
    ]); 
  //other elements here...
}

 public function setObjectManager(ObjectManager $objectManager)
{
    $this->objectManager = $objectManager;
}

public function getObjectManager()
{
    return $this->objectManager;
} 

private function addInputFilter() 
{  
    //  inputfilter elements here...  
}
}

有人可以指导我如何解决这个问题。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的班级表格应该实现ObjectManagerAwareInterface

class AddForm extends Form implements ObjectManagerAwareInterface
{

}

因为类ObjectManagerAwareInterface的实例将从ObjectManager方法注入setObjectManager()