普通网友 2012-12-04 20:53
浏览 26
已采纳

CakePHP - 另一个关系表中的SUM数据

i have no idea in getting a result for sum of hours that staff has been attend to courses and assembly. Here are my database:-

staffs
+---+-------+-----------+
| id| Name  | Department|
+---+-------+-----------+
| 1 | Joe   | HR        |
| 2 | Andra | Support   |
| 3 | Khan  | HR        |
| 4 | Toll  | HR        |
| 5 | Gosh  | Support   |
| 6 | Rama  | Support   |
+---+-------+-----------+



attendances
+---+---------+-----------+--------------+
|id | name_id | course_id | assembly_id  |
+---+---------+-----------+--------------+
| 1 |    1    |  101      |              |
| 2 |    1    |  102      |              |
| 3 |    1    |           | 501          |
| 4 |    2    |  102      |              |
| 5 |    3    |           | 502          |
| 6 |    3    |  103      |              |
| 7 |    4    |  101      |              |
| 8 |    4    |           | 501          |
| 9 |    4    |  102      |              |
+---+---------+-----------+--------------+    


courses
+-------+-----------+-------+
| id    | name      | hours |
+-------+-----------+-------+
| 101   | courses_1 | 8     |
| 102   | courses_2 | 6     |
| 103   | courses_3 | 9     |
+-------+-----------+-------+


assembly
+-------+---------------+-------+
| id    | name          | hours |
+-------+---------------+-------+
| 501   | assembly_1    | 2     |
| 502   | assembly_2    | 4     |
+-------+---------------+-------+

In Model > Attendance i already add a relation:-

public $belongsTo = array('Staff','Course','Assembly'); 

In my Controller i try write this code:-

$view = $this->Staff->find('all',array(
'fields' => array('SUM(COALESCE(Course.hours,0))+SUM(COALESCE(Assembly.hours,0)) as ec_count'),'recursive'=>2));
$this->set('view', $view); 

But it will return an errors..(Seem like i do some wrong here)

How to write a codes with this condition and it will appears through a view like this:-

+---+-------+-----------+------------+
| id| Name  | Attend    | Total Hour |
+---+-------+-----------+------------+
| 1 | Joe   | course_1  |      16    |
|   |       | course_2  |            |
|   |       | assembly_1|            |
| 2 | Andra | course_2  |       6    |
| 3 | Khan  | assembly_2|      13    |
|   |       | course_3  |            |
| 4 | Khan  | course_1  |      16    |
|   |       | assembly_1|            |
|   |       | course_2  |            |
| 5 | Gosh  | -         |      0     |
| 6 | Rama  | -         |      0     |
+---+-------+-----------+------------+
  • 写回答

1条回答 默认 最新

  • douejuan9162 2012-12-05 01:11
    关注

    Unless you're doing Joins (see CakePHP Joins), you will not be able to get the SUMs like that, as CakePHP will actually run individual queries, then combine and return the data.

    I think you're best bet would be to keep track of the total hours in Staff.course_hours and Staff.assembly_hours fields within the 'staffs' table. You can still keep the individual rows in the Assembly.hours and Course.hours, but then on an afterSave() (see CakePHP afterSave) callback (or something similar), update the Staff hours fields.

    Then, you can just set up a virtual field (see CakePHP Virtual Fields) called total_hours that you can sort by, or display, or use for anything you need.

    OR, you could just build out the Joins and do it in a single (though slightly more complex) query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?