Doctrine ResultSetMapping忽略实体

时间:2015-11-18 18:33:31

标签: php doctrine

在学说中,我想从本机查询中获取多个实体。但我无法锻炼如何做到这一点。

我有3个实体:WBSOProjectEmployeeWBSOBooking。员工可以通过WBSOBooking实体记录他们的工作时间,该实体将EmployeeWBSOProject结合在一起,并且还有一个额外字段用于记录的小时数。

为了生成每个项目每位员工的工作时间概览,我编写了以下代码:

$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实体?我该如何解决这个问题?

0 个答案:

没有答案