yii2 Error: Call to a member function count() /offset()

折腾了两个小时,翻到官方论坛。按下面的修改。

控制器代码

......
$data = (new \yii\db\Query()) //Order::find()

            ->select(['od.id as id', 'cust.realname as memname', 'mp.categary_name as catename', 'od.status as status'])

            ->from('rf_member_order od')

            ->leftJoin('rf_member as cust', 'cust.id = od.member_id')

            ->leftJoin('rf_member_myproduct as mp', 'mp.id = od.product_id')

            ->andFilterWhere(['od.merchant_id' => $this->getMerchantId()])

            ->all();

$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]); $models = $data->offset($pages->offset) ->orderBy('id desc') ->limit($pages->limit) ->all();

......

调试报错

Error: Call to a member function count()

Error: Call to a member function andFilterWhere() 

Error: Call to a member function offset() 


原因和办法

去除代码最后的->all()。

调用all方法不会得到一个ActiveQuery实例,而是一个ActiveRecord数组。即使去换调换成count($data) ,还会报offset()的错误。

At the end of $query definition, you call ->all() method, so you will not have an ActiveQuery instance but an ActiveRecord arrays.Remove ->all() at the end of $query definition to continue adding conditions.

$data = (new \yii\db\Query()) //Order::find()

            ->select(['od.id as id', 'cust.realname as memname', 'mp.categary_name as catename', 'od.status as status'])

            ->from('rf_member_order od')

            ->leftJoin('rf_member as cust', 'cust.id = od.member_id')

            ->leftJoin('rf_member_myproduct as mp', 'mp.id = od.product_id')

            ->andFilterWhere(['od.merchant_id' => $this->getMerchantId()]);

            ->all();

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • 这一篇最主要是记录下命令,方便以后查找 使用Mysql 创建数据库 create database mysql_t...
    Treehl阅读 602评论 0 0
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,505评论 0 13
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,921评论 0 23
  • 一、SQL速成 结构查询语言(SQL)是用于查询关系数据库的标准语言,它包括若干关键字和一致的语法,便于数据库元件...
    shadow雨轩阅读 521评论 0 3