簡體   English   中英

多列的Yii關系STAT

[英]Yii relation STAT with multiple columns

我正在嘗試從Review表獲取STAT關系。 這是我的代碼

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)'),
    }

桌子看起來像這樣

make_code | model_code | year | overall_rating
01        | 02         | 2015 | 4.2
01        | 03         | 2014 | 4.0
01        | 02         | 2015 | 3.0

我想獲得具有相同make_codemodel_codeyear所有行的overall_rating平均值

例如,make_code 01,model_code 02和2015年。 (4.2+3.0 / 2)$model->avarageRating應該給我3.6

現在,當我使用$model->avarageRating;調用關系時$model->avarageRating; 我得到一個錯誤

preg_match() expects parameter 2 to be string, array given

知道我在做什么錯嗎?

編輯:

function getavarageRating() {
        $criteria = new CDbCriteria();
        $criteria->select='ROUND(AVG(overall_rating), 1) AS avg, COUNT(overall_rating) AS 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;
    }

我在我的模型中添加了此功能,並添加了workws。 但是我有一種使用關系的方法嗎?

似乎您獲得了結果的集合,因此您應該按索引訪問,嘗試按以下方式訪問第一個元素

 $model[0]->avarageRating;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM