获取当前主题

时间:2017-06-08 14:30:42

标签: php symfony

我如何获得当前的帖子?我正在尝试重定向到我发送投票的当前帖子,但我创建的这种方法重定向到此用户创建的最旧帖子。

public function ScoreAction(Request $request){

    $em = $this->getDoctrine()->getManager();

    $idPoster = $request->request->get('id_posterUser');
    $positive= $request->request->get('positive');
    $negative= $request->request->get('negative');

    $user=  $em->getRepository(User::class)->findOneById($idPoster);


    $topic = $em->getRepository(Topic::class)->findOneByUser($user->getId());

    $score = $usuari->getReputation();

    if ($positive!= null) {
        $score = $score + 1;
    }
    if($negative!= null){
         $score = $score - 1;
    }

    $user->setReputation($score );
    $em->persist($user);
    $em->flush();

     $redirect = $this->generateUrl('discutea_forum_post', array('slug' => $topic->getSlug())); 

return $this->redirect($redirect);


}

编辑:添加了我的解决方案。

解决方案

 public function ScoreAction(Request $request){

        $em = $this->getDoctrine()->getManager();

        $idTopic = $request->request->get('id_topic');       

        $idPoster = $request->request->get('id_poster');

        $positive= $request->request->get('positive');
        $negative= $request->request->get('negatiu');

        $user=  $em->getRepository(User::class)->findOneById($idPoster); 

        $topic = $em->getRepository(Topic::class)->findOneById($idTopic); 


        $score= $user->getReputation();

        if ($positive!= null) {
            $score= $score+ 1;
        }
        if($negative!= null){
             $score= $score- 1;
        }

        $user->setReputation($score);
        $em->persist($user);
        $em->flush();


        $redirect = $this->generateUrl('discutea_forum_post', array('slug' => $topic->getSlug())); 

        return $this->redirect($redirect);
    }

1 个答案:

答案 0 :(得分:1)

您正在按用户搜索主题:

$topic = $em->getRepository(Topic::class)->findOneByUser($user->getId());

findOneByUser - 构建查询WHERE user.id = :user_id LIMIT 1

要获得最后一个主题,您需要添加订购。

$topic = $em->getRepository(Topic::class)->findOneBy(
    array('user' => $user), 
    array('id' => 'desc')
);