开发者

Symfony2 - Blog & Comment system - How to persist a Comment

I am learning how to make a basic Blog in Symfony2 and have come to a standstill when it comes to adding the comment system.

So far I can view the blog entries fine. Under a single blog post I am using Twig to render a form directly from a Controller:

{% render "PaulDemoBundle:Default:addComment" with { 'id': blog_entry.id } %}

As you can see I am populating the ID with the Id of the blog being viewed, in order to allow me to correctly attach the comment to the blog entry.

Below is the Action in my Controller:

public function addCommentAct开发者_如何学Cion(Request $request, $id)
{
    $add_comment = new Comment();
    $add_comment->setBlogId($id);

    $form = $this->createFormBuilder($add_comment)
            ->add('blog_id', 'hidden')
            ->add('author', 'text')
            ->add('comment', 'textarea')
            ->getForm();

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

        if ($form->isValid()) {
            $comment = $form->getData();
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($comment);
            $em->flush();

            return $this->redirect($this->generateUrl('_index'));
        }
    }

    return $this->render('PaulDemoBundle:Default:add_comment.html.twig', array('form' => $form->createView(), 'id' => $id));
}

here you can see that the Controller can pickup the Request data and the blog_id ($id). The form is generated with a hidden field that holds the blog_id.

Viewing the form HTML source, you get this:

<form action="/Symfony/web/app_dev.php/add_comment/3" method="post" >

<div id="form">
<input type="hidden" id="form__token" name="form[_token]" value="d4adb511709259d0df921a0a1b969cee0df83630" />
<input type="hidden" id="form_blog_id" name="form[blog_id]" value="3" />
<div><label for="form_author" class=" required">Author</label><input type="text" id="form_author" name="form[author]" required="required" value="" /></div>
<div><label for="form_comment" class=" required">Comment</label><textarea id="form_comment" name="form[comment]" required="required"></textarea></div></div>
<input type="submit" />

Again, you can see that the blog_id has been passed through (3).

Now, filling in the Author and Comment and hitting Submit, I get an error. It seems to think the blog_id is NULL. I have done a var_dump (and commented out the persist stuff) and the form request data shows that the blog_id is set to 3.

I even typecast $id to (int) as the form data shows it as a string (this shouldnt be a problem as it should be made an INT when it is persisted to the DB.

Can anyone see the problem?

Basically I have a M:1 Comment:Blog relationship and I need to somehow tell the application what blog is being viewed and pass along the ID (which I am doing atm, but it's broken ofc!)

edit: the var_dump:

object(Paul\DemoBundle\Entity\Comment)#67 (6) { 
["id":"Paul\DemoBundle\Entity\Comment":private]=> NULL 
["blog_id":"Paul\DemoBundle\Entity\Comment":private]=> string(1) "3"
["blog":"Paul\DemoBundle\Entity\Comment":private]=> NULL 
["author":"Paul\DemoBundle\Entity\Comment":private]=> string(8) "Mr Jones" 
["comment":"Paul\DemoBundle\Entity\Comment":private]=> string(4) "test"
["created":"Paul\DemoBundle\Entity\Comment":private]=> NULL } 


Finally got it figured out. Just had to get the blog entry via GetRepository and use that to link the comment and blog together.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜