-3

我知道很多人都问过这个问题,但甚至没有人回答: 这个回应。对于如何将当前用户的 id 获取到存储库中的 dql 语句中,这不是一个很好的回应。我看到这么多谈论它不是逻辑或者这是一个糟糕的选择,那么将当前用户的 id (如参数)用于 dql 语句或 QueryBuilder 有什么好处,这是我所做但不起作用的:

我的控制器:

 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();

}

我的服务:

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

我的存储库:

    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();

}

如果我选择 $currentid=1; 那就行了 但我需要连接的用户 ID。

感谢您的帮助和任何其他更改逻辑的建议,我会很高兴!

4

1 回答 1

1

像这样定义您的存储库方法:

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();
}

然后在控制器中传递属于当前登录用户的学生实体,例如:

$modeles=$em->getRepository("AcmeMyBundle:GroupMatiere")
        ->findGroupByStudent($this->getUser()->getStudent());
于 2015-09-16T18:01:52.980 回答