让我说我有部门,大学实体如跟
class Department{
private $name;
private $studentCount;
/**
*@ORM\ManyToOne(targetEntity="\CAPP\CollegeBundle\Entity\College", inversedBy="departments")
*@ORM\JoinColumn(name="college_id", referencedColumnName="id", nullable=false)
**/
private $college
}
class College
{
private $name;
private $departmentCount;
/**
*@ORM\OneToMany(targetEntity="\CAPP\DepartmentBundle\Entity\Department", mappedBy="college", cascade={"all"}, orphanRemoval=true)
*/
private $departments
}
当我编写一个doctrine查询构建器来获取具有以下查询的所有部门时
METHOD1:
return $this->getQb()->select("dp")
->from("DepartmentBundle:Department", "dp")
->getQuery()
->getResult()
;
我的结果包含所有字段,但当我尝试访问特定字段name
和college
时,请使用以下查询
METHOD2:
return $this->getQb()->select("dp.name, dp.college")
->from("DepartmentBundle:Department", "dp")
->getQuery()
->getResult()
;
我收到错误
col 10 near' college FROM':错误:无效的PathExpression。必须是StateFieldPathExpression。
我在这里犯了什么错误,学说在query method 1
上获取了各个大学实体的所有部门,但当我尝试query method 2
时,为什么会抛出错误。
答案 0 :(得分:1)
如果不是纯粹用于优化,则使用partial object的学说不鼓励。 你应该按照预期使用学说:完全面向对象。
$this->getQb('dp')
->select("dp")
->from("DepartmentBundle:Department", "dp")
如果你真的想使用partial:
$this->getQb('dp')
->select("partial dp.{name,college}")
->from("DepartmentBundle:Department", "dp")
答案 1 :(得分:0)
return $this->getQb('dp')->select('IDENTITY(dp.name)', 'IDENTITY(dp.college)')
->from("DepartmentBundle:Department", "dp")
->getQuery()
->getResult()
;
如果dp.name或dp.college是复合键,那么您需要添加IDENTITY以使查询起作用并解决问题
答案 2 :(得分:0)
不确定这是否是最佳答案,但我只是抓住该集合,然后使用get / set方法来提取信息...
$department = $this->getQb()->select("dp")
->from("DepartmentBundle:Department", "dp")
->getQuery()
->getResult();
$name = $department->getName();
$college = $department->getCollege();