我使用教义 ODM + Zend 形式,并且在水合方面遇到问题。我定义了一个集合"存储",其中包含"storeName"等元素和另一个字段集,如"地址"或"联系人"。像"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",
...
}
}
]
}
我发现,我的基地字段集公司商店字段集的水合器在水合物(数组 $data,$object)中得到了错误的$data。 $object是正确的。 所以。它不是真正的水合作用工作错误,而是我认为形式$data的设置......但我不知道为什么。我只是将数据设置为表单,如$form->setData($form),并且不会覆盖任何表单方法。所有其他补水器(在字段集中)似乎都得到了正确的$data并且工作正常。有人有想法吗?
这就是我正在做的事情。我的表单只是添加一个字段集公司商店字段集。字段集"用作基本字段集"。此字段集有一个集合元素,如下所示:
公司商店字段集:
$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我不修改任何补水器。
我的主要文档模型公司嵌入了这样的$stores
公司:
/**
* @var 'Doctrine'Common'Collections'ArrayCollection
* @ODM'EmbedMany (targetDocument="'Application'Model'Mongo'Company'Store")
*/
private $stores = array();
我的嵌入式文档存储添加了这样的元素
商店:
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;
模型联系人和地址只包含类属性 + 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);
}
问题是我在存储模型构造函数中为联系人和地址添加了新实例。那是错误的。