在学说中,我想从本机查询中获取多个实体。但我无法锻炼如何做到这一点。
我有3个实体:WBSOProject
,Employee
和WBSOBooking
。员工可以通过WBSOBooking
实体记录他们的工作时间,该实体将Employee
与WBSOProject
结合在一起,并且还有一个额外字段用于记录的小时数。
为了生成每个项目每位员工的工作时间概览,我编写了以下代码:
$query = "
select
project.id as project_id,
project.name,
employee.id as employee_id,
employee.bsn,
employee.name,
sec_to_time(sum(time_to_sec(wbsoBooking.total))) as balance
from wbso_projects project
inner join wbso_bookings as wbsoBooking on wbsoBooking.project_id = project.id
inner join employees as employee on employee.id = wbsoBooking.employee_id
group by project.year, project.name, employee.id, employee.name
";
$rsm = new ResultSetMapping();
$rsm
->addEntityResult("Kloktijden\\Model\\Entities\\WBSOProject", "project")
->addFieldResult("project", "project_id", "id")
->addFieldResult("project", "name", "name")
->addEntityResult("Kloktijden\\Model\\Entities\\Employee", "employee")
->addFieldResult("employee", "employee_id", "id")
->addFieldResult("employee", "bsn", "bsn")
->addFieldResult("employee", "name", "name")
->addScalarResult("balance", "balance");
$result = Model::getEntityManager()
->createNativeQuery($query, $rsm)
->getResult();
我想要的结果是这样的数组:
array(
[0] => array(
[0] => Kloktijden\\Model\\Entities\\WBSOProject,
[1] => Kloktijden\\Model\\Entities\\Employee,
"balance" => "00:00"
)
)
但不知怎的,Employee
实体被忽略了,所以我的结果如下:
array(
[0] => array(
[0] => Kloktijden\\Model\\Entities\\WBSOProject,
"balance" => "00:00"
)
)
所以,我的问题是:为什么我的结果集不包含Employee
实体?我该如何解决这个问题?