Yii2 Query does not return column sum

廉价感情. 提交于 2019-12-12 05:08:10

问题


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()


来源:https://stackoverflow.com/questions/34701451/yii2-query-does-not-return-column-sum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!