Yii2 Query does not return column sum

2019-08-31 16:44发布

问题:

I have this code, example:

$rows = Category::find()
->select('id_category, SUM(p.comments * 10) AS num')
->leftJoin('post p', 'p.id = post_id')
->groupBy('id_category')
->one();

This exist: $rows->id_category But this not exist: $rows->num

回答1:

You need a field named 'num' in your Category model otherwise you can't access to this value. add the public var $num this way

class Category extends \yii\db\ActiveRecord
{

    public $num;

    /**
    * @inheritdoc
    */
    public static function tableName()


标签: mysql yii2