ZF2 - 绑定嵌套字段集/集合

时间:2013-11-18 15:37:54

标签: forms zend-framework2 fieldset

我有两个与实体规范具有多对多单向关系的实体(元产品和选项)。

Metaproduct和Option之间的关系是一对多的。

Metaproduct的Fieldsets和Forms的代码如下:

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);
...

但是当我尝试在该表单上打印绑定对象时,会抛出以下Expect:

文件

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

1 个答案:

答案 0 :(得分:2)

请看这个问题:

嵌套集合和字段集中的问题

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