美文网首页PHP经验分享PHP实战
yii2 Error: Call to a member fun

yii2 Error: Call to a member fun

作者: ZeroScience | 来源:发表于2019-08-14 10:03 被阅读1次

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

控制器代码

......
$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();

相关文章

网友评论

    本文标题:yii2 Error: Call to a member fun

    本文链接:https://www.haomeiwen.com/subject/beuwjctx.html