0

我使用 ZF2+Doctrine+DoctrineMongoODM 模块。我已将Person文档嵌入到House文档中:

/**
 * @ODM\Document
 */
class Custelement{
    /** @ODM\EmbedOne(targetDocument="Person") */
    protected $person;

所以

#Document is binded to form
$form->bind($document);. 
#Common hydrator is used
$form->setHydrator(new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($docManager)); 

文档的公共字段House被保存和填充得很好。我使用带名称的 fieldsetperson来编辑嵌入的文档字段,因此有一组带有name=person[firstName]和的输入元素name=person[lastName]

嵌入文档的字段已保存但未填充到表单中。

我找到了一种解决方法 - 只需通过字段$vals = (array) $element->getValue();集对象获取值,然后

$name = preg_replace("/^(.*)\[(.*)\]$/", "\\2", $elem->getName()); $elem->setValue($vals[$name]);对于每个字段集元素。

有更好的解决方案吗?

4

1 回答 1

0

在 zf 手册的帮助下,我找到了我错过的东西。我应该为字段集设置 Hydrator 和 Object:http: //framework.zend.com/manual/2.0/en/modules/zend.form.collections.html#creating-fieldsets

$person->setHydrator(
    new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($docManager)
);
$person->setObject(new \Cab\Document\Person());

我认为我应该用填充对象调用 setObject 。不是。只是空对象。

于 2014-07-02T10:30:36.330 回答