Symfony 2 Doctrine ODM MongoDB持有水合物文件

时间:2014-06-04 10:05:49

标签: php mongodb symfony doctrine-orm

当我想要保留$ notice文档时,它会保存$ contexteUtilObj,但它不会将$ contexteUtilObj与$ notice文档相关联。

//Controller
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$notice = $dm->getRepository('WikiludoNoticeBundle:Notice')->find($id);
$contexteUtils = array();
$notice->setContexteUtilisations($contexteUtils);
if (count($form['context']) > 0) {
    foreach ($form['context'] as $key => $context) {
        if ($key == 0 and ! $context['valeur']) {
            break;
        }
        if (!$context['valeur']) {
            continue;
        }
        if ($context['id'] and ! is_numeric($context['id'])) {
            $contexteUtilObj = $dm->getRepository('WikiludoNoticeBundle:ContexteUtilisation')->find($context['id']);
        } else {
            $contexteUtilObj = new \Wikiludo\NoticeBundle\Document\ContexteUtilisation();
        }
        $contexteUtilObj->setContexteUtilisation1($context['valeur']);
        $notice->addContexteUtilisations($contexteUtilObj);
    }
}
$dm->persist($notice);
$dm->flush();

...

//Notice Document Structure
/**
 * @MongoDB\ReferenceMany(
 *      targetDocument="Wikiludo\NoticeBundle\Document\ContexteUtilisation",
 *      cascade="all"
 * )
 */
protected $contexteUtilisations;

1 个答案:

答案 0 :(得分:0)

抱歉,我解决了我的问题。我必须这样做:

$dm->flush($notice);