链式查询

 


 

例子

$userModel = new UserModel();

$userModel
    ->field('uid, nickname, avatar, add_time')
    ->where(['type' => 2])
    ->order('add_time DESC')
    ->limit(10)
    ->select();

 

设置表名 table

$model->table('timo_album')->where(['uid' => 1009])->select()

 

设置字段 fields

$model->fields('id, nickname')->select()

 

设置条件 where

$model->where(['uid' => 10000, 'status' => ['between', 0, 3]])->select()
$model->where('uid > 2018 AND uid < 3000')->select()

 

排序 order

$model->order('sort ASC, id DESC')->select()

 

分组 group

$model->fields('cate_id, count(*) count')->group('cate_id')->select()

 

返回条数 limit

$model->limit(20)->select()
$model->limit('10, 20')->select()

 

分页 page

$p = 1;
$page = ['p' => $p, 'limit' => 20];
$userModel = new UserModel();
$users = $userModel->fields('id, nickname, avatar')
    ->where(['type' => 2, 'status' => 1])
    ->page($page)
    ->select();
  
print_r($users);

上面会打印出:
Array
(
    [p] => 1
    [limit] => 20
    [total] => 108
    [total_page] => 6
)

看见没,$page增加了两个元素total和total_page,框架会自动统计出这些信息,对于前端写分页挺好的,但是,
这样就增加了SQL的统计查询,很多时候我们只需要第一页有这些数据,所有,模型的select方法有一个可选参数,当传入参数不等于1的时候,
框架就不会去统计这些数据

$p = 2;
$page = ['p' => $p, 'limit' => 20];
$userModel = new UserModel();
$users = $userModel->fields('id, nickname, avatar')
    ->where(['type' => 2, 'status' => 1])
    ->page($page)
    ->select($p);
  
print_r($users);

上面会打印出:
Array
(
    [p] => 1
    [limit] => 20
)

 

返回一列 column

$questionModel->fields('title')->where(['uid' => 10001])->column()

 

返回多行数据 select

参数

默认等于1,当传入非1的值时,就不会去统计分页信息