yii2分页

方法1

public function getList($table,$field='*',$where=[],$order='',$limit='',$page='')
{
    try {
        $lists = (new \yii\db\Query());
        $lists->select($field)->from($table)
        ->join('LEFT JOIN','jh_users b','b.user_id=a.admin_id')
        ->join('LEFT JOIN','jh_attendance c','a.employee_number=c.employee_number and a.start_time=c.start_time and a.end_time =c.end_time');
 
        if($where) $lists->where($where);
        if($order) $lists->orderBy($order);
        if($limit) $lists->limit($limit);
        if($page) $lists->offset(($page-1)*$limit);
 
        $lists = $lists->all();
    } catch (\Error $e){
        return ['status'=>'Error','error_msg'=>$e->getMessage()];
    } catch (\Exception $e) {
        return ['status'=>'Exception','error_msg'=>$e->getMessage()];
    } catch (\Throwable $e) {
        return ['status'=>'Throwable','error_msg'=>$e->getMessage()];
    }
    return ['status'=>'successful','data'=>$lists];
}
方法2

public function getPaginate()
{
    $query = Performance::find();
    $pagination = new Pagination([
        'defaultPageSize' => 200,
        'totalCount' => $query->count()
    ]);
    $pers = $query->orderBy('per_id desc')
        ->offset($pagination->offset)
        ->limit($pagination->limit)
        ->all();
    $pers= array_map(function($record) {
        return $record->attributes;
    }, $pers);
    return array('code'=>0,'msg'=>'successful','count'=>$pagination->totalCount,'data'=>$pers);
}
 控制器中使用

use app\models\Performance;
use Yii;
$where = [
    '>','per_id',50
];
$field = 'a.per_id,
          a.per_name,
          a.start_time,
          a.end_time,
          b.user_id,
          b.employee_number,
          b.username_ch,
          b.email,
          c.attendance_rate';
$data = Performance::getList('jh_performance a',$field,$where,'per_id asc',10,2);
$data = Performance::getPaginate();
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容