Symfony2,学说和形式 - 以单一形式持有1:*关系双方的数据 [英] Symfony2, Doctrine, and forms - Persisting the data of both sides of a 1:* relationship in a single form

查看:99
本文介绍了Symfony2,学说和形式 - 以单一形式持有1:*关系双方的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Symfony2项目中,我与一些实体有一个相当经典的1:*关系(我们称之为BlogPost和评论,即使NDC阻止我说出真相)。我一直在负责修改当前存在的编辑现有的意见,以便它可以修改博文的某些方面和形式。我不完全确定如何去做,特别是Symfony2和Doctrine如何处理他们的数据绑定。



现在,我填充并绑定到伪代码保护无辜者):

 根据传入的请求获取BlogPost 
抓取所有评论相关到BlogPost

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

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

的foreach($评论作为$评论){
$ doctrine->坚持($评论);
}
}

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

As你可以看到,我已经发送BlogPost到视图,我知道我可以通过将它添加到我的CommentsType类添加到表单,我只是不知道如何数据绑定它一切正常。

解决方案

如果您有 $ blogPost ,只需保留它,与注释一样。最后还要刷新:

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

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

foreach($ comments as $ comment){
$ doctrine-> persist($ comment);
}
$ doctrine-> persist $博客帖子);
$ doctrine->冲洗();
}

$返回这 - >呈现( blah.html.twig,阵列(博客 => $志文章,评论 => $评论,形式 => $形式 - > CreateView的());


In my Symfony2 project, I have a rather classic 1:* relationship with some of my entities (let's call them BlogPost and Comments, even though a NDC prevents me from saying what they really are). I've been tasked with modifying the form that currently exists to edit existing Comments so it can modify certain aspects of the BlogPost as well. I'm not entirely sure how to go about it, specifically with how Symfony2 and Doctrine handle their data binding.

Right now, I populate and bind to the form with (pseudo-code to protect the innocent):

Grab the BlogPost based on the incoming request ID
Grab all Comments related to BlogPost

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

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

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

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

As you can see, I already send the BlogPost to the view. And I know I can add it to the form by including it in my CommentsType class. I'm just not sure how to data bind it all properly.

解决方案

If you have the $blogPost, just persist it, the same as the comments. Also flush at the end:

$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());

这篇关于Symfony2,学说和形式 - 以单一形式持有1:*关系双方的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆