【问题标题】:ZF2 - Binding Nested Fieldsets/CollectionsZF2 - 绑定嵌套字段集/集合
【发布时间】:2014-01-09 15:03:52
【问题描述】:

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

元产品和选项之间的关系是一对多的。

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


    }   

元产品.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))

【问题讨论】:

    标签: forms zend-framework2 fieldset


    【解决方案1】:

    请看这个问题:

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

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

    【讨论】:

    • 嵌套的字段集和集合实际上存在问题。 Tnx
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 2015-02-05
    相关资源
    最近更新 更多