2013-11-18 2 views
1

У меня есть два объекта (Metaproduct and Options), которые имеют многостороннее однонаправленное отношение с сущностью Спецификация.ZF2 - Связывание вложенных наборов/сборников

Отношение между Metaproduct и Option является одним-ко-многим.

Код для Fieldsets и форм для Metaproduct заключается в следующем:

MetaproductFieldset.php

namespace Bundle\Fieldset; 

class MetaproductFieldset extends EntityUsingFieldset implements InputFilterProviderInterface{ 
... 
    public function __construct(ObjectManager $objectManager) 
    { 
     $this->add(array(
      'type' => 'Zend\Form\Element\Collection', 
      'name' => 'options', 
      'options' => array(
       'label' => 'Options', 
       'count' => 1, 
       'allow_add' => true, 
       'allow_remove' => true, 
       'should_create_template' => true, 
       'target_element' => new OptionFieldset($objectManager), 
      ), 
      'attributes' => array(
       'id' => 'options', 
      ), 
     )); 

     $this->add(array(
      'type' => 'Zend\Form\Element\Collection', 
      'name' => 'specifications', 
      'options' => array(
       'label' => 'Specifications', 
       'count' => 1, 
       'allow_add' => true, 
       'allow_remove' => true, 
       'should_create_template' => true, 
       'target_element' => new SpecificationFieldset($objectManager), 
      ), 
      'attributes' => array(
       'id' => 'specifications', 
      ), 
     )); 

OptionFieldset.php

namespace Bundle\Fieldset; 

    class OptionFieldset extends EntityUsingFieldset implements InputFilterProviderInterface{ 

     public function __construct(ObjectManager $objectManager) 
     { 

      $this->setObjectManager($objectManager); 
      parent::__construct('option'); 

      $this->setHydrator(new DoctrineHydrator($objectManager))->setObject(new \Bundle\Entity\Option()); 
$this->add(array(
      'type' => 'Zend\Form\Element\Collection', 
      'name' => 'specifications', 
      'options' => array(
       'label' => 'Specifications', 
       'count' => 1, 
       'allow_add' => true, 
       'allow_remove' => true, 
       'should_create_template' => true, 
       'target_element' => new SpecificationFieldset($objectManager), 
      ), 
      'attributes' => array(
       'id' => 'specifications', 
      ), 
     )); 

SpecificationFieldset.php

namespace Bundle\Fieldset; 

use Doctrine\Common\Persistence\ObjectManager; 
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator; 
use Zend\InputFilter\InputFilterProviderInterface; 

class SpecificationFieldset extends EntityUsingFieldset implements InputFilterProviderInterface{ 


    public function __construct(ObjectManager $objectManager) 
    { 
     $this->setObjectManager($objectManager); 
     parent::__construct('specification'); 

     $this->setHydrator(new DoctrineHydrator($objectManager))->setObject(new \Bundle\Entity\Specification()); 

     $this->add(array(
      'type' => 'Zend\Form\Element\Hidden', 
      'name' => 'id' 
     )); 

     $this->add(array(
      'type' => 'DoctrineModule\Form\Element\ObjectSelect', 
      'name' => 'label', 
      'options' => array(
       'label'   => 'Label', 

       'object_manager' => $objectManager, 
       'target_class' => 'Bundle\Entity\Label', 
       'property'  => 'value', 
       'empty_option' => '--- please choose ---' 
      ), 
     )); 

     $this->add(array(
      'type' => 'Zend\Form\Element\Text', 
      'name' => 'value', 
      'options' => array(
       'label' => 'Value' 
      ) 
     )); 


    } 

Metaproduct.php

namespace Bundle\Form; 
... 
class Metaproduct extends Form { 

public function __construct(ObjectManager $objectManager){ 

    parent::__construct('metaproduct-form'); 
    $this->setHydrator(new DoctrineHydrator($objectManager)); 
    $mpFieldset = new MetaproductFieldset($objectManager); 
    $mpFieldset->setUseAsBaseFieldset(true); 
... 

Но когда я пытаюсь напечатать привязки объекта на этой форме, следующий Expection является забросил:

Файл

zendframework\library\Zend\Form\Fieldset.php:439 

Сообщение

Zend\Form\Fieldset::setObject expects an object argument; received "Array" 

Trace

#0 C:\projects\acuradoria-zend\vendor\zendframework\zendframework\library\Zend\Form\Element\Collection.php(549): Zend\Form\Fieldset->setObject(Array) 
#1 C:\projects\acuradoria-zend\vendor\zendframework\zendframework\library\Zend\Form\Fieldset.php(601): Zend\Form\Element\Collection->extract() 
#2 C:\projects\acuradoria-zend\vendor\zendframework\zendframework\library\Zend\Form\Form.php(854): Zend\Form\Fieldset->extract() 
#3 C:\projects\acuradoria-zend\vendor\zendframework\zendframework\library\Zend\Form\Form.php(292): Zend\Form\Form->extract() 
#4 C:\projects\acuradoria-zend\module\Bundle\src\Bundle\Controller\Plugin\FormService.php(42): Zend\Form\Form->bind(Object(Bundle\Entity\Metaproduct)) 

ответ

2

Пожалуйста, смотрите этот вопрос:

Проблемы в вложенных коллекциях и fieldsets

https://github.com/zendframework/zf2/issues/5640

+0

Было на самом деле проблема с вложенным fieldsets и коллекциями. Tnx –

Смежные вопросы