Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2、条令和表格-将1:*关系双方的数据保存在单一表格中_Php_Symfony_Doctrine - Fatal编程技术网

Php Symfony2、条令和表格-将1:*关系双方的数据保存在单一表格中

Php Symfony2、条令和表格-将1:*关系双方的数据保存在单一表格中,php,symfony,doctrine,Php,Symfony,Doctrine,在我的Symfony2项目中,我与我的一些实体有着非常经典的1:*关系(我们称它们为BlogPost和Comments,尽管NDC阻止我说出它们的真实面目)。我的任务是修改当前存在的表单以编辑现有评论,这样它也可以修改博客文章的某些方面。我不完全确定该怎么做,特别是Symfony2和Doctrine如何处理它们的数据绑定 现在,我使用(保护无辜者的伪代码)填充并绑定表单: 正如你所看到的,我已经将BlogPost发送到视图。我知道我可以将它添加到表单中,方法是将它包含在我的CommentsTyp

在我的Symfony2项目中,我与我的一些实体有着非常经典的1:*关系(我们称它们为BlogPost和Comments,尽管NDC阻止我说出它们的真实面目)。我的任务是修改当前存在的表单以编辑现有评论,这样它也可以修改博客文章的某些方面。我不完全确定该怎么做,特别是Symfony2和Doctrine如何处理它们的数据绑定

现在,我使用(保护无辜者的伪代码)填充并绑定表单:


正如你所看到的,我已经将BlogPost发送到视图。我知道我可以将它添加到表单中,方法是将它包含在我的CommentsType类中。我只是不知道如何正确地绑定所有数据。

如果您有
$blogPost
,只需将其保存,与注释相同。最后也要冲洗:

$form = $this->createForm(new CommentsType(), array('comments' => $comments));

if ($request->getMethod() == "POST")
{
    $form->bind($request);

    foreach($comments as $comment) {
        $doctrine->persist($comment);
    }
    $doctrine->persist($blogPost);
    $doctrine->flush();
}

return $this->render('blah.html.twig', array('blog' => $blogPost, 'comments' => $comments, 'form' => $form->createView());

这是一个解决方案,但是如果只
持久化
$blogPost
并在关系上使用
cascade=all
,不是更好(或者更容易)吗?如果您可以访问orm.yml或实体/注释代码,cheesemacfly的建议可能会起作用。我在cascade方面的经验并不一致,这主要是由于本文介绍的模型比较复杂。
$form = $this->createForm(new CommentsType(), array('comments' => $comments));

if ($request->getMethod() == "POST")
{
    $form->bind($request);

    foreach($comments as $comment) {
        $doctrine->persist($comment);
    }
    $doctrine->persist($blogPost);
    $doctrine->flush();
}

return $this->render('blah.html.twig', array('blog' => $blogPost, 'comments' => $comments, 'form' => $form->createView());