【发布时间】:2013-06-10 04:30:23
【问题描述】:
我想获取用户所在的学校,但由于某种原因,我只能通过对不相关的学校表运行查询来访问它。这是我的代码:
这不起作用(在控制器内):
$schoolsEnrolled = $this->getUser()->getSchools();
结果是一个数组,其中包含一个 School 对象,其所有属性都为 null(由于某种原因,id 除外)。
这确实有效(在控制器内):
//unrelated query
$repository = $this->getDoctrine()->getRepository('AcmeMainBundle:School');
$query = $repository->createQueryBuilder('s')->getQuery();
$schools = $query->getResult();
//the query I care about
$schoolsEnrolled = $this->getUser()->getSchools();
结果是所需的学校数组。
相关方法如下:
在学校课堂上:
public function getSchools(){
$schools = array();
foreach ($this->schoolHasUsers as $key=>$schoolHasUser){
$schools[] = $schoolHasUser->getSchool();
}
return $schools;
}
在 SchoolHasUser 类中:
public function getSchool()
{
return $this->school;
}
如果没有不相关的查询,我如何才能让我关心的查询正常工作?
【问题讨论】:
-
也许你不需要无关查询,只需要激活存储库:
$this->getDoctrine()->getRepository('AcmeMainBundle:School');? -
我试过没有查询,但它不起作用。我需要 $schools = $query->getResult();行。
标签: symfony controller doctrine