zf2表单收集水合作用错误$ hydator

时间:2015-06-17 16:50:19

标签: php mongodb zend-framework zend-form doctrine-odm

我正在使用Doctrine ODM + Zend Form并且遇到水化问题。 我用像“storeName”这样的元素定义了一个Collection“Stores”,还有一个像“address”或“contactPerson”这样的Fieldsets。 像“storeName”这样的简单元素将被保存得很好,但像“地址”这样的字段集总是会保存最后的条目数据。

所以我保存的Json-Object看起来像这样

    {
        "_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",
...
                }
            }
        ]
    }

我发现,我的base-Fieldset CompanyStoresFieldset 的Hydrator在hydrate中获取了错误的$ data(数组$ data,$ object)。 $ object是正确的。所以。它不是真正的水合作用错误,它设置的形式$数据我想...但我不知道为什么。我只是将数据设置为表格,如 $ form-> setData($ form),我不会覆盖任何表单方法。 所有其他Hydrator(在fieldsets中)似乎得到了正确的$ data并且工作正常。 有人有想法吗?

所以这就是我正在做的事情。 我的表单只添加了一个Fieldset CompanyStoresFieldset 。 Fieldset“用作基本字段集”。这个Fieldset有一个Collection Element,如下所示:

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

StoreEntityFieldset 添加简单的元素和字段集元素。

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

    }

在表单和字段集中,我只需设置 Zend \ Stdlib \ Hydrator \ ClassMethods 我不修改任何Hydrators。

My Main Document Model Company嵌入了这样的$ store

公司

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

My EmbeddedDocument 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;

模型 ContactPerson 地址只包含类属性+ getArrayCopy和exchangeArray的setter和getter。

getArrayCopy和exchangeArray如下所示:

    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 个答案:

答案 0 :(得分:0)

问题是我在商店模型构造函数中为ContactPerson和Address添加了新实例。那是错的..

相关问题