dongliu6848 2016-10-30 15:05
浏览 16
已采纳

允许用户仅对评论投票一次[symfony2]

I have a table vote and comment I would to allow a user to vote only once for a comment_id. For now a user can vote multiple times for a comment.

I would like before to persist user in the table vote to verified if the current user_id has already voted for a specify comment_id, if yes the vote will not be persist.

Controller.php

public function voteAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();
    $comment = $em->getRepository('ApplicationSonataUserBundle:Comment')->findOneBy(array('id' => $id));
    $entity = new Vote();
    $entity->setUser($this->get('security.token_storage')->getToken()->getUser());
    $entity->setVotecomment($comment);
    $entity->setCreatedAt(new \DateTime());
    $em->persist($entity);
    $em->flush();
    return $this->redirect($this->generateUrl('userShow', array(
    'entity' => $entity->getVotecomment()->getRecipient(),
    'slug' =>  $entity->getVotecomment()->getRecipient()->getId(),
        )));
}

.

vote.php

    <?php

    namespace Application\Sonata\UserBundle\Entity;
    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\Collection;
    use Symfony\Component\Validator\Constraints as Assert;
    use Doctrine\Common\Collections\ArrayCollection;

    /**
     * @ORM\HasLifecycleCallbacks
     * @ORM\Entity
     * @ORM\Entity(repositoryClass="Application\Sonata\UserBundle\Entity\UserRepository")
     * @ORM\Table(name="vote")
     *
     */

    class Vote
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;


        /**
         * @var User
         * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="likes")
         */
        protected $user;

        /**
         * @var int
         *
         * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\Comment", inversedBy="votes", cascade={"persist"})
         * @ORM\JoinColumn(name="votecomment_id", referencedColumnName="id")
         *
         */
        private $votecomment;

        /**
         * @ORM\Column(name="createdAt", type="datetime", nullable=false)
         */
        protected $createdAt;


        public function __construct()
        {
            $this->setCreatedAt(new \DateTime());

            $this->votecomment = new ArrayCollection();
        }

}

.

twig file

<a class="fa fa-thumbs-o-up" href="{{ path('likecomment', {'entity': entity ,'slug': entity.recipient.id ,'id': entity.id}) }}"></a><b class="text-color">&nbsp; &nbsp;{{ entity.votes|length }}</b>
  • 写回答

3条回答 默认 最新

  • dongzhuo3059 2016-11-04 11:00
    关注

    Basically I needed just to check if the current user has already voted for a comment.

    Below an example of how I did it :

       public function voteAction(Request $request,$id)
        {
            $em = $this->getDoctrine()->getEntityManager();
    
    
            $comment = $em->getRepository('ApplicationSonataUserBundle:Comment')->findOneBy(array('id' => $id));
            $voteExist = $em->getRepository('ApplicationSonataUserBundle:Vote')->findOneBy([
                'user' => $this->getUser(),
                'votecomment' => $comment
            ]);
            $vote = new Vote();
            if(!$voteExist) {
    
                $vote->setUser($this->get('security.token_storage')->getToken()->getUser());
                $vote->setVotecomment($comment);
                $vote->setCreatedAt(new \DateTime());
                $em->persist($vote);
                $em->flush();
    
                return $this->redirect($this->generateUrl('userShow', array(
                    'entity' => $vote->getVotecomment()->getRecipient(),
                    'slug' => $vote->getVotecomment()->getRecipient()->getId(),
                )));
    
    
    
            } else {
    
                $em->remove($voteExist);
                $em->flush();
                //$this->get('session')->getFlashBag()->add('notice', 'You have already voted!');
    
            }
    
            return $this->redirect($this->generateUrl('userShow', array(
                'entity' => $comment->getRecipient(),
                'slug' => $comment->getRecipient()->getId(),
            )));
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址