如何重新启动用户投票

时间:2017-06-08 20:40:45

标签: php symfony

我为forumbundle的用户实施了投票系统。它工作正常,但我想重新设计,只是让用户投票1次。所以我创建了一个在我的bdd上投票的布尔值。 但我不知道如何避免该用户再次投票,但与此同时我想让他们撤消投票。

所以我做了这个功能:

readarray -t args < <(grep '^/etc/[a-z][A-Z]*' source.txt)
your_function "${args[@]}"

使用更改boolean更新我的数据库中的int nullable并生成此代码:

 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();
    $voted = $user->getVoted();
    if ($positive!= null) {
        $score= $score+ 1;
        $voted = 1;
    }
    if($negative!= null){
         $score= $score- 1;
         $voted = 1;
    }

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


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

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

1 个答案:

答案 0 :(得分:1)

$voted = $user->getVoted();

if ($voted) {
    //just redirect somewhere else, without making an actual vote
} else {
    //do your vote stuff
    //persist and flush etc
    //redirect somewhere else
}

这些方面的东西?

您是否尝试实现以下目标?:

$voted = $user->getVoted();

if ($voted) {
    if ($positive!= null) {
        $user->setReputation($user->setReputation()+2);
    }
    if ($negative!= null){
         $user->setReputation($user->setReputation()-2);
    }
} else {
    if ($positive!= null) {
        $user->setReputation($user->setReputation()+1);
    }
    if ($negative!= null){
         $user->setReputation($user->setReputation()-1);
    }
}

编辑:

试试这个

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