zf2 form collection hydration gets wrong $data in hydrator

426 views Asked by At

Im using doctrine ODM + Zend Form and have trouble with the Hydration. I define a Collection "Stores" with Elements like "storeName" and one more Fieldsets like "address" or "contactPerson". The simple Elements like "storeName" will be saved fine, but the fieldsets like "address" it always will be saved the last entry data.

So my saved Json-Object looks like this

    {
        "_id" : ObjectId("5541e8a50203ff4c1e00002a"),
        ...
        "stores" : [ 
            {
                "_id" : "12345678998",
                "storeName" : "test1",
                "openingTimes" : "123",
                "address" : {
                    "placeId" : "ChIJYbqFGmnKuEcRm-J84sWlfMY",
                    "street" : "Karolingerstraße 10",
                    "queryString" : "Karolingerstraße 10", ...
                },
                "contactPerson" : {
                    "salutation" : "1",
                    "firstname" : "foo",
                    "lastname" : "bar",
...
                }
            }, 
            {
                "_id" : "557fe9a92d86d",
                "storeName" : "test 3",
                "openingTimes" : "hgfhgfhgf",
                "address" : {
                    "placeId" : "ChIJYbqFGmnKuEcRm-J84sWlfMY",
                    "street" : "Karolingerstraße 10",
                    "queryString" : "Karolingerstraße 10", ...
                },
                "contactPerson" : {
                    "salutation" : "1",
                    "firstname" : "foo",
                    "lastname" : "bar",
...
                }
            }
        ]
    }

I figured out, that the Hydrator of my base-Fieldset CompanyStoresFieldset gets the wrong $data in hydrate(array $data, $object). $object are correct. So. Its not really the Hydration that works wrong, its the setting $data by Form i think... But i don´t know why. I just set the Data to the Form like $form->setData($form) and i don´t overwrite any Form Methods. All other Hydrators (in fieldsets) seems to get the right $data and works fine. Has anybody a Idea?

So here´s what i´m doing exactly. My Form just adds a Fieldset CompanyStoresFieldset. The Fieldset is "used as base fieldset". this Fieldset has one Collection Element that looks like this:

CompanyStoresFieldset:

$this
            ->setHydrator(new ClassMethods(false))
            ->setObject(new Company())
        ;

$this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'stores',
            'options' => array(
                'should_create_template' => true,
                'allow_add' => true,
                'allow_remove' => true,
                'create_new_objects' => true,
                'target_element' => array(
                    'type' => 'Application\Form\Fieldset\StoreEntityFieldset',
                ),
            ),
        ));

The StoreEntityFieldset adds simple Elements and Fieldset Elements.

StoreEntityFieldset:

        $this
            ->setHydrator(new ClassMethods(false))
            ->setObject(new Store())
        ;


        $this->add(array(
            'name' => 'storeName',
            ...
        ));

        //AddressFieldset
        $this->add(array(
            'name' => 'address',
            'type' => 'Application\Form\Fieldset\AddressFieldset',
        ));

        //ContactPersonFieldset
        $this->add(array(
            'name' => 'contactPerson',
            'type' => 'Application\Form\Fieldset\ContactPersonFieldset',
        ));

        $this->add(array(
            'name' => 'openingTimes',
            'type' => 'Textarea',
            ...
        ));

    }

In the Form and Fieldsets i just set Zend\Stdlib\Hydrator\ClassMethods I don´t modify any Hydrators.

My Main Document Model Company embedd $stores like this

Company:

/**
     * @var \Doctrine\Common\Collections\ArrayCollection
     * @ODM\EmbedMany (targetDocument="\Application\Model\Mongo\Company\Store")
     */
    private $stores = array();

My EmbeddedDocument Store adds elements like this

Store:

class Store {

    /**
     *
     * @var string
     * @ODM\Id(strategy="NONE")
     */
    private $id;

    /**
     *
     * @var string
     * @ODM\String
     */
    private $storeName;

    /**
     *
     * @var string
     * @ODM\String
     */
    private $openingTimes;

    /**
     * @var \Application\Model\Mongo\Company\Address
     * @ODM\EmbedOne(targetDocument="\Application\Model\Mongo\Company\Address")
     */
    private $address;

    /**
     * @var \Application\Model\Mongo\Company\ContactPerson
     * @ODM\EmbedOne(targetDocument="\Application\Model\Mongo\Company\ContactPerson")
     */
    private $contactPerson;

The Models ContactPerson and Address just contains setter and getter for the class attributes + getArrayCopy and exchangeArray.

getArrayCopy and exchangeArray looks like this:

    public function getArrayCopy() {
        $ref = new \Zend\Stdlib\Hydrator\Reflection();
        return $ref->extract($this);
    }

    public function exchangeArray($data = array()){
        $ref = new \Zend\Stdlib\Hydrator\Reflection();
        return $ref->hydrate($data, $this);
    }
1

There are 1 answers

0
Sascha On BEST ANSWER

The Problem was I add new Instances for ContactPerson and Address in the Store Model Constructor. That was wrong..