1
votes

I have two entities (Metaproduct and Options) that have Many-To-Many Unidirectional relation with entity Specification.

The relation between Metaproduct and Option is One-To-Many.

The code for the Fieldsets and Forms for Metaproduct is the following:

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

But when I try to print bind an object on that form, the following Expection is throwed:

File

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

Message

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

1 Answers

2
votes

Please see this issue:

Problems in nested collections and fieldsets

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