Yii2 Query does not return column sum

2019-08-31 16:34发布

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

标签: mysql yii2
1条回答
我只想做你的唯一
2楼-- · 2019-08-31 17:14

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()
查看更多
登录 后发表回答