Php 如何限制用户投票

Php 如何限制用户投票,php,symfony,Php,Symfony,我在forumbundle上为我的用户实现了投票系统。它的工作原理很好,但我想重新限制,让用户投票1次。所以我在bdd上创建了一个布尔值。 但我不知道该如何避免用户再次投票,但同时我想让他们撤销投票 所以我做了这个函数: public function ScoreAction(Request $request){ $em = $this->getDoctrine()->getManager(); $idTopic = $request->request-&

我在forumbundle上为我的用户实现了投票系统。它的工作原理很好,但我想重新限制,让用户投票1次。所以我在bdd上创建了一个布尔值。 但我不知道该如何避免用户再次投票,但同时我想让他们撤销投票

所以我做了这个函数:

 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);
}
在我的数据库上更新int-nullable的change boolean,并将此代码设置为:

类似的东西

您是否正在努力实现以下目标

$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;
    }
}

所以你做了这个函数。。。然后呢?什么不起作用?@Daniel我不能否认用户可以撤消他的投票。我不知道symfony的情况,但逻辑应该是:“投票表中存在与用户关联的行?如果是,用户已经投票,用户不能再次投票,用户可以撤消(删除行)投票。如果不存在,可以投票。”我试着实施了一个取消投票的选项,但没有成功。你尝试了什么?张贴代码?预期结果与实际结果的对比是什么…但是我想让用户更改他的投票@Samjanssens你想再次改变他们的“分数”吗?这不意味着他们又要投票了吗?你所说的“改变他们的投票”是什么意思?假设你是一个用户,想给其他用户投正面的票,但你点击错误,投了负面的票。你想改变你的投票@Samjanssens是的,但是如果我是一个用户,我已经投票了+,现在我投票-,我的“分数”属性应该更新吗?如果我以前投票了+(你的分数是+1),我将它改为-,它应该改为-2而不是-1吗?这就是你想要达到的目标吗?
$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;
    }
}