yii2 一对多关系的对分页造成的影响

下面代码中关联descies时,匹配较多,造成分页数不对,需要加条件限制:

$model = User::find()
->joinWith('app')
->joinWith(['descies' => function($query){
return $query->where(['user_desc_type_id'=>7]);
}])
->where($condition)
->andFilterWhere($condition_pay_start)
->andFilterWhere($condition_sex);
$pagination = new Pagination([
'defaultPageSize' => 10,
'totalCount' => $model->count(),
]);
1.可在model中建立关联时限定:
  1. public function getBooks()
  2.     {
  3.         return $this->hasMany(Item::className(), ['owner_id' => 'id'])->onCondition(['category_id' => 1]);
  4.     }

 2.代码中限定:方法如上:

posted on 2018-03-05 12:33  小帅豹  阅读(305)  评论(0编辑  收藏  举报