方法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();
yii2分页
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Yii 2.0最显著的特征之一就是引入了命名空间,因此对于自定义类的引入方式也同之前有所不同。这篇文章讨论一下如何...