【问题标题】:How to get the current id from the specific topic?如何从特定主题中获取当前 id?
【发布时间】:2017-05-16 07:53:59
【问题描述】:

我有一个主题论坛,每个主题都可以被其他用户评论。我创建的关系是:

class Comment
{
    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Topic", inversedBy="comments")
     */
    private $topic;
}

还有班级主题:

class Topic
{
     /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Comment", mappedBy="topic")
     * @ORM\JoinColumn(name="comment_id", referencedColumnName="id")
     */
    private $comments;  
}

问题是,当我创建评论时,cmets 表中的 topic_id 列为空。我不确定如何获取当前主题 ID 并将其设置为评论。这是我在 CommentController 中的 addCommentAction() 函数

     /**
     * @Route("/comment/add", name="comment_add")
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function addCommentAction(Request $request)
    {
        $comment = new Comment();

        $form = $this->createForm(CommentType::class, $comment);
        $form->handleRequest($request);

        if ($form->isValid()) {

            $comment->setDateCreated(new \DateTime());

            $em = $this->getDoctrine()->getManager();
            $em->persist($comment);
            $em->flush();


            $this->addFlash(
                'notice',
                'Comment Added Successfully !'
            );
        }
        return $this->render('comments/comment.add.html.twig', array(
            'commentsForm' => $form->createView()
        ));
    }

树枝模板中的路径:

<a href="{{ path('comment_add') }}" class="btn btn-lg btn-default">Leave a Comment</a>

【问题讨论】:

  • 我认为您应该通过评论表单中的隐藏输入或通过 url GET 参数(表单的操作属性)传递topic_id。如果您在处理表单的同一操作中呈现表单,则无论如何您都需要在 url 中使用主题 ID。

标签: php mysql symfony


【解决方案1】:

快完成了 :) 试试关注

如果主题已经存在

$em = $this->getDoctrine()->getManager();
$topic = $em->getRepository('YouBundle:Topic')->find( $topic_id );

if(false === $topic instanceof topic )
{
 // not found ...
}

$comment = new Comment();
$comment->setTopic( $topic )
// createForm....

如果没有,则在 DB 中查找一个,创建一个

$topic = new Topic();
$comment = new Comment();
$comment->setTopic( $topic );

UPD: 正如@Jakub Matczak 所说。您可能必须在表单中添加一个带有 topic_id 的隐藏字段,因为您不想获得包含所有可用主题的下拉列表。

buildForm 方法中的 CommentForm 类中

$builder->add('topic', HiddenType:class, []);

【讨论】:

    【解决方案2】:

    您必须在评论实体上设置主题。在您的路由中,您必须添加主题的 id,然后 Symfony 将自动获取带有 ParamConverter 的主题实体

    类似的东西应该可以工作:

    /**
     * @Route("/{id}/comment/add", name="comment_add")
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function addCommentAction(Request $request, Topic $topic)
    {
        $comment = new Comment();
        $comment->setTopic($topic);
    
        $form = $this->createForm(CommentType::class, $comment);
        $form->handleRequest($request);
    
        if ($form->isValid()) {
    
            $comment->setDateCreated(new \DateTime());
    
            $em = $this->getDoctrine()->getManager();
            $em->persist($comment);
            $em->flush();
    
    
            $this->addFlash(
                'notice',
                'Comment Added Successfully !'
            );
        }
        return $this->render('comments/comment.add.html.twig', array(
            'commentsForm' => $form->createView()
        ));
    }
    

    【讨论】:

    • 我已经尝试过您的示例,但现在在 twig 模板中出现异常,即缺少一些强制参数(“id”)来生成 URL。我将添加我在上面的问题中使用的模板路径。
    【解决方案3】:

    注意事项: $form = $this->createForm(CommentType::class, $comment); 不应该是“评论”吗?我在您的描述中没有看到任何 CommentType 类。

    此外,当您添加新评论时,您需要以某种方式传递 topicId。 我会为“/comment/add”做一个 POST 操作,并在正文中发送主题 ID。

     // Rest of the imports...
     use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
    
     /**
     * @Route("/comment/add", name="comment_add")
     * @Method({"POST"})
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function addCommentAction(Request $request)
    {
        $comment = new Comment();
    
        $form = $this->createForm(CommentType::class, $comment);
        $form->handleRequest($request);
    
        if ($form->isValid()) {
    
            $comment->setDateCreated(new \DateTime());
    
            $em = $this->getDoctrine()->getManager();
    
            $topicId = $request->request->get('topicId');
            $topic = $em->getRepository('Bundle:Topic')->find($topicId); // retrieve the topic id from the corresponding repository of the topic entity
            $comment->setTopic($topic); // setting the topic to the comment
    
            $em->persist($comment);
            $em->flush();
    
    
            $this->addFlash(
                'notice',
                'Comment Added Successfully !'
            );
        }
        return $this->render('comments/comment.add.html.twig', array(
            'commentsForm' => $form->createView()
        ));
    }
    
    You need also to add a repository from which you will retrieve the Topic objects. You need just an empty class named TopicRepository that should extend EntityRepository from Doctrine.
    

    除此之外,您还应该在从客户端执行 POST 请求时发送“topicId”。

    【讨论】:

    • 我在表单目录中有一个名为 CommentType 的类,我在其中构建表单,所以它正在工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    相关资源
    最近更新 更多