Calculating Fields In A Relational Query

I am pounding my head on the desk trying to get calculated results from a HAS_MANY relation. Specifically I want to add quantities together

in model:




	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'submissionitems' => array(self::HAS_MANY, 'RebateSubmissionItems', 'RebateProductID'),

			

		);

	}




In my controller:




		$criteria = new CDbCriteria;

		$criteria->select = 'Product,UPC, StartDate, EndDate, DeadlineDate,ProductID';

		$criteria->with = array('submissionitems'=>array('select'=>"sum('submissionitems.Quantity') as TotalRebates"));



in my view:




$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'rebate-products-grid',

	'dataProvider'=>$dataProvider,

	'filter'=>$model,

	'columns'=>array(

		array(

	        'class'=>'CLinkColumn',

	        'labelExpression'=>'$data->Product',

	        'urlExpression'=>'"../rebateSubmissionItems/indexRebate?thisproduct=".$data->Product."&thisrebate=".$data->ProductID',

	        'header'=>'Rebate Product'

	      ),

		'UPC',		

		'StartDate',

		'EndDate',

		'DeadlineDate',

		'TotalRebates'

	),

));




The TotalRebates column is empty. Any ideas where I went wrong??

Hi Michael Sullivan!

Have you tried the ‘self::STAT’ on the relations() method in your model?




...

/**

 * @return array relational rules.

 */

public function relations()

{

    // NOTE: you may need to adjust the relation name and the related

    // class name for the relations automatically generated below.

    return array(

        'submissionItemsCount' => array(self::STAT, 'RebateSubmissionItems', 'RebateProductID'),

    );

}

...



PS: not tested!