0

Let's say I want to have a provide CActiveDataProvider for a CGridView. I need to put a SUM(invitesCount) AS invites into a Provider result. How to retrieve it? I guess I cannot just use $dataProvider->invites?

Joe
  • 2,551
  • 6
  • 38
  • 60

2 Answers2

2

You need to specify the following in your relationinvites

'invites '=>array(self::BELONGS_TO, 'CampaignFund', 'campaign_id', 'select' => 'SUM(invitesCount)'),

and use this relation in your criteria.

Nisha haridas
  • 332
  • 1
  • 8
  • 22
  • Thank you. Is it good idea to add realtions every time we need some field? Is that efficient? I noticed seconds before read your answer I can just define new public properties and do the select with `AS` pointint to those properties. Not sure which way is the better one? – Joe Apr 04 '13 at 09:04
1

Several other options:

  1. Use CStatRelation

    invitesCount=>array(self::STAT,'Invites','foreign_key_field');
    
  2. The addition of a public property can work. However, the field would only be set if you altered the default find query to include this new condition. This can be done by overriding defaultScope() or creating a new scope and using it whenever invitesCount is required.

  3. Another option would be to create a database view from the required query and create a new Model from that database view.

topher
  • 14,790
  • 7
  • 54
  • 70
  • Actually you don't have to define new `scope`. You can simply change `search` method to accept `CDbCriteria` object as a parameter with default value `null` and then merge default criteria with criteria from parameter. – Joe Apr 04 '13 at 11:45