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