前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TP6学习笔记

TP6学习笔记

作者头像
黄啊码
发布2021-09-26 10:37:18
1.1K0
发布2021-09-26 10:37:18
举报
文章被收录于专栏:黄啊码【CSDN同名】

具体文档:https://www.kancloud.cn/dengyulin/think/58288

进行原生的SQL查询 $Model->query('SELECT * FROM think_user WHERE status = 1');

如果你当前采用了分布式数据库,并且设置了读写分离的话,query方法始终是在读服务器执行,因此query方法对应的都是读操作,而不管你的SQL语句是什么

`Model = new Model() // 实例化一个model对象 没有对应任何数据表 Model->execute("update think_user set name='thinkPHP' where status=1");`如果你当前采用了分布式数据库,并且设置了读写分离的话,execute方法始终是在写服务器执行,因此execute方法对应的都是写操作

子查询

新版新增了子查询支持,有两种使用方式:1、使用select方法 当select方法的参数为false的时候,表示不进行查询只是返回构建SQL,例如: // 首先构造子查询SQL subQuery = model->field('id,name')->table('tablename')->group('field')->where(where)->order('status')->select(false);2、使用buildSql方法model->field('id,name')->table('tablename')->group('field')->where(model->table(

多表查询

Subscribe外链Order表,在Subscribe这个模型中增加

public function orders()

{

return $this->belongsTo('app\common\model\Order', 'subscribe_id', 'subscribe_id');

}

控制器中使用

list = this->model->where(

->withJoin([

'orders' => ['order_amount', 'pay_status','deliver_status']

], 'LEFT')

->page(

->select();

得到某条记录的单个字段值

this->model->field('id')->where(

条件更新:

save = this->model->whereIn('subscribe_id',ids)->update(

save = this->model->update(post, ['time_id' =>

UserAddress::edit(addressInfo, id, 'id')

res = this->model->where('config_name',"DoctorVisit")->save(array("value"=>

//新增记录

UserAddress::create($addressInfo);//新增后可获取整条记录

this->model-save(

按条件删除

$this->model = new DoctorTime();

row = this->model->whereIn('id',

row->isEmpty() &&

try {

save = row->delete();

} catch (\Exception $e) {

$this->error('删除失败');

}

with使用

1.withjoin是一种方法 static function getVisitResult() { result = self::withjoin('userItems','LEFT') ->where('userItems.id','=',1) ->order('id desc') ->select()->toArray(); return result = self::with(['userItems' => functionquery->where('id',1); }]) ->with('companyItems') -(>order('id desc') ->select()->toArray(); return

字符串模式查询(采用_string 作为查询条件) 数组条件还可以和字符串条件混合使用,例如:User = M("User"); // 实例化User对象 map['name'] = 'ok'; map['_string'] = 'status=1 AND score>10'; User->where(

请求字符串查询方式 请求字符串查询是一种类似于URL传参的方式,可以支持简单的条件相等判断。map['id'] = array('gt','100'); map['_query'] = 'status=1&score=100&_logic=or';得到的查询条件是:id>100 AND (status = '1' OR score = '100')

复合查询 复合查询相当于封装了一个新的查询条件,然后并入原来的查询条件之中,所以可以完成比较复杂的查询条件组装。 例如: where['name'] = array('like', '%thinkphp%'); where['title'] = array('like','%thinkphp%'); where['_logic'] = 'or'; map['_complex'] = where; map['id'] = array('gt',1);查询条件是 (id>1)AND( (namelike'%thinkphp%')OR(titlelike'%thinkphp%') ) 复合查询使用了_complex作为子查询条件来定义,配合之前的查询方式,可以非常灵活的制定更加复杂的查询条件。 很多查询方式可以相互转换,例如上面的查询条件可以改成:where['id'] = array('gt',1); where['_string'] = ' (name like "%thinkphp%") OR ( title like "%thinkphp") ';最后生成的SQL语句是一致的。

区间查询

where[] = array('start_time','between',today_start,

where['end_time']=array('ELT',today_end);

$doctor_time = new DoctorTime();

time_info = doctor_time->whereBetween('start_time',"today_start,today_end")->whereBetween('end_time',"today_start,today_end")->find();

从thinkphp 5.1.x后vendor的使用方法发生变化,文档又没有详细说明。官方真的太坑了!

在thinkPHP 5.1.X后新版取消了Loader::import方法以及import和vendor助手函数,推荐全面采用命名空间方式的类以及自动加载机制,如果必须使用请直接改为php内置的include或者require语法。

原来的import(“Vendor.Classes.PHPExcel.IOFactory”);或Vendor(‘phpoffice.phpexcel.Classes.PHPExcel.IOFactory’);方法已经不再使用。

请使用下面的方法:

thinkphp6 使用vendor中的第三方库

require_once('/data/www/xxxxxxx/vendor/PHPExcel/PHPExcel.php');

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/06/29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 子查询
  • with使用
相关产品与服务
分布式数据库 TDSQL
分布式数据库TDSQL是腾讯打造的一款企业级数据库产品,具备强一致高可用、全球部署架构、高 SQL 兼容度、分布式水平扩展、高性能、完整的分布式事务支持、企业级安全等特性,同时提供智能 DBA、自动化运营、监控告警等配套设施,为客户提供完整的分布式数据库解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档