ZF2 — привязка вложенных наборов полей/коллекций

У меня есть два объекта (метапродукт и параметры), которые имеют однонаправленное отношение «многие ко многим» со спецификацией объекта.

Отношение между метапродуктом и опцией — «один ко многим».

Код для наборов полей и форм для метапродукта следующий:

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',
            ),
        ));

СпецификацияFieldset.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'
            )
        ));


    }   

Метапродукт.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);
...

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

Файл

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

Сообщение

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

След

#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))

person Lucas Tourinho Cavalcante    schedule 18.11.2013    source источник


Ответы (1)


Пожалуйста, ознакомьтесь с этой проблемой:

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

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

person BRAVO    schedule 09.01.2014
comment
На самом деле была проблема с вложенными наборами полей и коллекциями. Tnx - person Lucas Tourinho Cavalcante; 10.01.2014