0
点赞
收藏
分享

微信扫一扫

TP6的注意事项

豆丁趣 2023-03-07 阅读 73


create()可以默认根据主键插入

save()需要指定id名称叫做id,create不需要

获取自增id

$insert_data = $this->model->create($data);
echo $insert_data->id;

//echo $this->model->getLastSql();

ThinkPHP5框架

1 whereOr 方法
2
3 使用whereOr 方法进行OR 查询:
4 Db::table('think_user')
5 ->where('name','like','%thinkphp')
6 ->whereOr('title','like','%thinkphp')
7 ->find();
8 多字段相同条件的OR 查询可以简化为如下方式:
9 Db::table('think_user')
10 ->where('name|title','like','%thinkphp')
11 ->find();

where 内部实现 精选or 条件拼接,一直没有找到合适的方法,不像tp3版本可以使用采用以下方法:

$map['user_id'] = 1;
$map['status'] = 0;
$or_map['user_id'] = 1;
$or_map['audit']=['in',['1,2']];
$where_main['_complex'] = array(
$map,
$or_map,
'_logic' => 'or'
);

$this->table->where($where_main)->select();

tp5中采用闭包的方式:

$map['user_id']=1;
$map['status']=0;
$or_map['user_id']=$map['user_id'];
$or_map['audit']=['in',['1,2']];
$list = Db::name('table')->where(function ($query) use ($map) {
$query->where($map);
})->whereOr(function ($query) use ($or_map) {
$query->where($or_map);
})->limit(0,$pn*$page_size)->select();SELECT * FROM `tp_table` WHERE ( `user_id` = '1' AND `status` = 0 ) OR ( `user_id` = '1' AND `audit` IN ('1,2') )

举报

相关推荐

0 条评论