dtjpnd7517 2015-06-17 16:50
浏览 58
已采纳

zf2形式集合水合作用在水合物中得到错误的$数据

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条回答 默认 最新

  • douyue3800 2015-06-29 10:27
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想通过python处理sybase IQ库的一些数据,但是不知道sybase IQ库怎么连接,之前连接都是用linux上的 dbisql
  • ¥15 安装llama-cpp-python==0.1.83失败
  • ¥20 管道轴向耦合水击问题
  • ¥60 补全networkx TODO部分。
  • ¥15 有内推吗,云计算linux运维方向
  • ¥30 sort cuteSV.vcf by bcftools用IGV可视化出现报错
  • ¥100 SOS!对STK中导出的天体图像进行质心提取有没有人做过啊
  • ¥15 python 欧式距离
  • ¥15 运行qteasy报错
  • ¥15 遗传算法解决有工序顺序约束的大规模FJSP问题