版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zchqq/article/details/82843147
方法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();