php - Yii relation STAT with multiple columns -
i'm trying stat relation review table. code
public function relations() { return array( 'avaragerating' => array(self::stat, 'reviews', array('make_code'=>'make_code', 'model_code'=>'model_code', 'year'=>'year'), 'select' => 'avg(overall_rating)'), }
the table this
make_code | model_code | year | overall_rating 01 | 02 | 2015 | 4.2 01 | 03 | 2014 | 4.0 01 | 02 | 2015 | 3.0
i want overall_rating
average rows have same make_code
, model_code
, year
example, make_code 01, model_code 02 , year 2015. (4.2+3.0 / 2)
, $model->avaragerating
should give me 3.6
right now, when call relation using $model->avaragerating;
error
preg_match() expects parameter 2 string, array given
any idea i'm doing wrong?
edited:
function getavaragerating() { $criteria = new cdbcriteria(); $criteria->select='round(avg(overall_rating), 1) avg, count(overall_rating) total'; $criteria->addcondition("make_code=:make_code"); $criteria->addcondition("model_code=:model_code"); $criteria->addcondition("year=:year"); $criteria->params = array(':make_code' => $this->make_code, ':model_code' => $this->model_code, ':year' => $this->year); $query = reviews::model()->find($criteria); return $query; }
i added function in model, , workws. there way use relations instead?
seems collection result should access index , try accessing first element
$model[0]->avaragerating;
wiki
Comments
Post a Comment