Turgs
Turgs

Reputation: 1799

Grouped aggregations with Yii STAT?

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?

Upvotes: 3

Views: 1277

Answers (1)

acorncom
acorncom

Reputation: 5955

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.

Upvotes: 1

Related Questions