-3

I know that this question was asked by many but no one was answered even : This reponse. is not a good reponse for how do i get current user's id into a dql statement in Repository. i see so much talking about it's not logic or it's a bad choice so what 's the good one to use the current user's id like a param into a dql statement or QueryBuilder and this what i do but doesn't work :

My Controller :

 public  function GetGroupsByStudentAction()
{
    $em=$this->getDoctrine()->getManager();
    $modeles=$em->getRepository("AcmeMyBundle:GroupMatiere")
            ->findGroupByStudent();
    return($this->render("AcmeMyBundle:GroupMatiere:list.html.twig",array("modeles"=>$modeles)));
}    

    public function ConnectedUserIdAction()
{  $user = $this->container->get('security.token_bag')->getToken()->getUser();

   return $user->getId();

}

MyServices :

<service id="serviceGroupe" class="Acme\MyBundle\Controller\GroupMatiereController"/>

MyRepository :

    public function findGroupByStudent()
{ 
  //   $currentid = i dont know how i call the methode from the service;
    $query=$this->getEntityManager()
        ->createQuery("SELECT m from AcmeMyBundle:GroupMatiere m WHERE m.idGroupe=?1")
        ->setParameter(1, $currentid);
    return $query->getResult();

}

That's work if i choose for the $currentid=1; But i need connected user id .

Thanks For Help and any other suggestion to change the logic i will be happy !

Community
  • 1
  • 1
Dmk
  • 88
  • 1
  • 11

1 Answers1

1

Define your repository method like this:

public function findGroupByStudent($student)
{ 
    $query=$this->getEntityManager()
        ->createQuery("SELECT m from AcmeMyBundle:GroupMatiere m WHERE m.idGroupe=?1")
        ->setParameter(1, $student->getId());
    return $query->getResult();
}

And then in the controller pass the Student entity that belongs to the currently logged in user, e.g.:

$modeles=$em->getRepository("AcmeMyBundle:GroupMatiere")
        ->findGroupByStudent($this->getUser()->getStudent());
LorenzSchaef
  • 1,523
  • 10
  • 16