Grouped aggregations with Yii STAT?

蹲街弑〆低调 提交于 2019-12-12 12:43:01

问题


I have a Yii STAT Relation that's defined to provide a grouped SUM result, however when I access the relation in my View, the only value is the latest single value rather than each value.

For example, here's my relation:

'total_salaries_by_job' => array(
    self::STAT, 
    'Employee', 
    'department_id', 
    'select' => 'job_type_id, SUM(salary)', 
    'group'=>"job_type_id"
) 

This generates the following SQL:

SELECT
  department_id AS c
, job_type_id
, SUM(salary) AS s 
FROM Employee AS t
WHERE t.department_id = 1 
GROUP BY 
  department_id
, job_type_id 

Running that manually, the result set is:

c     | job_type_id    | s
------+----------------+---------
1     | 1              | 233000
------+----------------+---------
1     | 2              | 25000
------+----------------+---------
1     | 3              | 179000

However, in my view, if I do the following:

<pre>
<?php print_r($department->total_salaries_by_job); ?>
</pre>

The result is simply: 179000, whereas I was expecting it to be an array with 3 elements.

Is returning just 1 value the way STAT relations work or is there something else I need to be doing?
Is it possible to do what I'm attempting?


回答1:


You can do what you are after, but you can't use a STAT relationship to do it. Rather, use a Normal HAS_MANY relationship and use your same select statement.



来源:https://stackoverflow.com/questions/11394954/grouped-aggregations-with-yii-stat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!