我需要知道执行此操作的最佳方法...我的数据库中有一个表,对于这篇文章,我们将其称为 TC,它只有几个我感兴趣的字段:
id, daydate, noteBy_id, and totaltime
noteBy_id 被分配给用户表,该表再次只有我正在寻找的几个项目......
id, first_name, last_name, user_name, department, parentcompany
部门参考了第三个表,该表仅在字段中具有id
和departmentName
...
到目前为止,我使用查询生成器创建了这个:
$query = $qb->select('t', 'u')
->leftJoin('t.noteBy', 'u', 'WITH', 'u.id = 't.noteBy')
->where('t.daydate BETWEEN :start AND :end')
->andWhere('u.parentcompany = :pid')
->andWhere('u.department = :dep)
->groupBy('u.id')
->setParameter('start', $start)
->setParameter('end', $end)
->setParameter('dep', $department)
->getQuery();
现在,这个查询按原样工作,但它返回:
Array
(
[0] => Array
(
[id] => 6
[in1] => DateTime Object
(
[date] => 2012-04-17 09:10:00
[timezone_type] => 3
[timezone] => America/Denver
)
[out1] =>
[in2] =>
[out2] =>
[in3] =>
[out3] =>
[totaltime] => 0.00
[daydate] => DateTime Object
(
[date] => 2012-04-17 09:10:00
[timezone_type] => 3
[timezone] => America/Denver
)
[noteBy] => Array
(
[username] => j
[usernameCanonical] => j
[email] => me@me.me
[emailCanonical] => me@me.me
[enabled] => 1
[salt] =>
[password] =>
[credentialsExpired] =>
[credentialsExpireAt] =>
[id] => 124
[full_name] =>
[first_name] => J
[last_name] => B
[socialsecurity] =>
[phone] =>
[phone2] =>
[address1] =>
[address2] =>
[city] =>
[state] =>
[zip] =>
[startdate] =>
[notes] =>
)
)
)
这是错误的,因为它只有 1 个项目,在运行的查询中它应该在数据库中有 2 个。
它应该是这样的:
[0]
(
[id] => '1'
[first_name] => 'j'
[last_name] => 'b'
[username] => 'jb'
[department] => 'some department'
[total_time] => SUM of all records in TC.total_time for user id 1
)
[1]
(
[id] => '2'
[first_name] => 'frank'
[last_name] => 'blew'
[username] => 'fb'
[department] => 'some department'
[total_time] => SUM of all records in TC.total_time for user id 2
)
但是,如果用户 id 在 TC 表中至少有 1 个匹配项,则结果数组中应该只有条目......任何关于弄清楚这个查询的帮助表示赞赏..