Petruza
Petruza

Reputation: 12276

Using grouped aggregate SQL in a Yii relation in a CGridView

I'm trying to do something very similar to what thomas.mery posted, but can't make it work, here's the code, can you see what I'm doing wrong?

I have these tables:

The Event Model has this relation:
'InvitationCodes' => array( self::HAS_MANY, 'InvitationCode', 'event' )

I added this to the Event::search() that was generated by Gii:
$criteria->with = array( 'InvitationCodes' => array( 'select' => 'COUNT(*) as qtty' ) );
$criteria->together = true;
$criteria->group = 't.id';

And then in the CGridView of Events, I wan to show the qtty field, so I include in the columns:
'InvitationCodes.qtty'

The column comes out with the header, but no values. What am I doing wrong?

Upvotes: 0

Views: 1091

Answers (1)

marcovtwout
marcovtwout

Reputation: 5269

You could add a public property $qtty to your InvitationCodes-model and get the values through $theResultRow->InvitationCodes->qtty (or InvitationCodes.qtty in string like configuration)

But in your case, using a statistical query relation is much better: http://www.yiiframework.com/doc/guide/1.1/en/database.arr#statistical-query

Upvotes: 1

Related Questions