dqyy38265 2016-12-21 12:27
浏览 75
已采纳

删除元素之前的条件[symfony2]

A user can have an album that contains many pictures, he can delete the picture or upload a new picture. I would like the album to have 1 or more picture,but never null (0). So before the user to delete a picture I want to be able to check if the amount of picture is more than one, if it's less than one display a flashbag message notifying the user that he can't have 0 picture in his album.

This what I have done:

/**
 * @Security("has_role('ROLE_USER')")
 */
public function avatarUserDeletedAction($id)
{
    $em = $this->getDoctrine()->getManager();
    $entity = $em->getRepository('ApplicationSonataUserBundle:Avatar')->find($id);

    if ($this->container->get('security.token_storage')->getToken()->getUser() != $entity->getUser() || !$entity)
        return $this->redirect($this->generateUrl('avatarUser'));

    $em->remove($entity);
    $em->flush();

    return $this->redirect($this->generateUrl('avatarUser'));
    //return $this->redirectToRoute('avatarUser');
}

ADD:

/**
 * @Security("has_role('ROLE_USER')")
 */
public function avatarUserDeletedAction(Request $request,$id)
{
    $em = $this->getDoctrine()->getManager();
    $entity = $em->getRepository('ApplicationSonataUserBundle:Avatar')->find($id);

    if ($entity == 1) {
        $request->getSession()
            ->getFlashBag()
            ->add('success', 'You cannot have less than 1 picture');
    } else{
        if ($this->container->get('security.token_storage')->getToken()->getUser() != $entity->getUser() || !$entity)
            return $this->redirect($this->generateUrl('avatarUser'));
        $em->remove($entity);
        $em->flush();
        }
    return $this->redirect($this->generateUrl('avatarUser'));
    //return $this->redirectToRoute('avatarUser');
}

I have the error below:

Notice: Object of class Application\Sonata\UserBundle\Entity\Avatar could not be converted to int

  • 写回答

2条回答 默认 最新

  • drrqwokuz71031449 2016-12-21 12:47
    关注

    What about this?

    public function avatarUserDeletedAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('ApplicationSonataUserBundle:Avatar')->find($id);
        $qb = $em->createQueryBuilder();
        $qb->select('count(entity.id)');    
        $qb->from('ApplicationSonataUserBundle:Avatar','entity');
    
        $count = $qb->getQuery()->getSingleScalarResult();
    
        if ($this->container->get('security.token_storage')->getToken()->getUser() != $entity->getUser() || !$entity)
            return $this->redirect($this->generateUrl('avatarUser'));
    
        if($count >= 2) {
            $em->remove($entity);
            $em->flush();    
        }
        else {
            //generate error indicating user that can't have zero elements in the album 
        }
    
        return $this->redirect($this->generateUrl('avatarUser'));
        //return $this->redirectToRoute('avatarUser');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢