2014-05-17 45 views
1

我正在使用zendframework 2和doctrine 2.我想从我的数据库中的值中填充MultiCheckbox的值。 我得到了技术来源:https://github.com/doctrine/DoctrineModule/blob/master/docs/form-element.mdzendframework 2表单从数据库中填充MultiCheckbox的值

namespace Users\Form; 
use Zend\Form\Form; 
use DoctrineModule\Persistence\ObjectManagerAwareInterface; 
use Doctrine\Common\Persistence\ObjectManager; 
class addForm extends form implements ObjectManagerAwareInterface 
{ 
protected $objectManager; 
public function setObjectManager(ObjectManager $objectManager) 
{ 
    $this->objectManager = $objectManager; 
} 

public function getObjectManager() 
{ 
    return $this->objectManager; 
} 
public function __construct($name = null) 
{ 
    parent::__construct('add'); 
    $this->setAttribute('method', 'post'); 
    $this->setAttribute('enctype','multipart/formdata'); 
      $this->add(array(
    'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox', 
    'name' => 'option', 
    'options' => array(
    'label' => 'Options Véhicule', 
    'object_manager' => $this->getObjectManager(), 
       'target_class' => 'Users\Entity\optionsvehicule', 
       'property'  => 'property' 
      , ))); 

我收到错误消息: 没有对象管理器设置。

+0

它是通过依赖注入来设置的吗? – alex

+0

@alex怎么可能'__construct'调用了方法'$ this-> getObjectManager()',这是以前不能设置的。 – AlexP

+0

没有在构造函数中看到它...这就是为什么它是一个问题 – alex

回答

1

我都试过,发现类似的错误。经过一番搜索,我发现解决方案发布在https://github.com/doctrine/DoctrineModule/issues/175。哪些工作。

对于实现你需要做这样的

在Module.php添加方法getFormElementConfig一些变化:

public function getFormElementConfig() 
{ 
    return array(
     'invokables' => array(
      'addForm' => 'Users\Form\addForm', 
     ), 
     'initializers' => array(
      'ObjectManagerInitializer' => function ($element, $formElements) { 
       if ($element instanceof ObjectManagerAwareInterface) { 
        $services  = $formElements->getServiceLocator(); 
        $entityManager = $services->get('Doctrine\ORM\EntityManager'); 

        $element->setObjectManager($entityManager); 
       } 
      }, 
     ), 
    ); 
} 

在窗体类addForm.php,用init方法代替构造函数:

namespace Users\Form; 
use Zend\Form\Form; 
use DoctrineModule\Persistence\ObjectManagerAwareInterface; 
use Doctrine\Common\Persistence\ObjectManager; 
class addForm extends form implements ObjectManagerAwareInterface 
{ 
    protected $objectManager; 
    public function setObjectManager(ObjectManager $objectManager) 
    { 
     $this->objectManager = $objectManager; 
    } 

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

    //public function __construct($name = null) 
    public function init() 
    { 
     $this->setAttribute('method', 'post'); 
     $this->setAttribute('enctype','multipart/formdata'); 
     $this->add(array(
      'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox', 
      'name' => 'option', 
      'options' => array(
       'label' => 'Options Véhicule', 
       'object_manager' => $this->getObjectManager(), 
       'target_class' => 'Users\Entity\optionsvehicule', 
       'property'  => 'property' 
      , ))); 

在您的控制器类中,通过服务定位器呼叫表单obejct:

//$form = new addForm(); 
$forms = $this->getServiceLocator()->get('FormElementManager'); 
$form = $forms->get('addForm'); 
+0

谢谢,请为什么我们添加此行;'property'=>'property'这有一个错误:在对象“Vehicules \ Entity \ optionsvehicule”中找不到属性“property” –

+0

实际上,这应该是'property'=>' propertyName',其中propertyName是每个复选框显示的字段,因此它可能类似于'property'=>'username'' –

+0

非常感谢。 –

0

$objectManager属性未定义。

这是因为您在__construct()中立即调用$this->getObjectManager()方法,并在设置变量之前。

表格取决于对象管理器上的;所以你可以将它作为一个构造函数参数来添加,这将确保它在使用类之前被设置。

此外,构造函数应该只用于设置对象的初始属性和状态,请使用init()修改表单元素。

class addForm extends Form 
{ 
    protected $objectManager; 

    public function __construct(ObjectManager $objectManager) 
    { 
     parent::__construct('add-form'); 

     $this->objectManager = $objectManager; 
    } 

    // The form element manager will call `init()` 
    // on the form so we can add the elements in this method 
    public function init() { 
     //.... 
     $this->setAttribute('method', 'post'); 
     $this->setAttribute('enctype','multipart/formdata'); 

     // $this->add(.... 
     // more elements added here 
    } 

} 

最后一件事是注册一个工厂,实际上确实注射

class Module { 

    public function getFormElementConfig() { 
     return array(
      'factories' => array(
       'ModuleName\Form\FooForm' => function($formElementManager) { 
        $serviceManager = $formElementManager->getServiceLocator(); 
        $objectManager = $serviceManager->get('ObjectManager'); 

        $form = new Form\FooForm($objectManager); 

        return $form; 
       }, 
      ), 
     ); 
    } 
} 
+0

我真的很困惑,我认为所有窗体的元素被添加到窗体,因为构造函数与“$ this-> add(... “我会张贴我所有的表格来澄清事情 –

+0

谢谢你,我做了你所说的一切,但我得到了一个错误:属性”属性“无法找到对象”Vehicules \ Entity \ optionsvehicule“!! –