多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ## **路由配置** ### **配置文件地址** ``` project 应用部署目录 ├─conf 配置文件目录 │ └─db.php 数据库配置信息 ``` ### **配置内容** >[info]配置信息采用数组模式 多数据库模式采用添加数组模式实现 ``` <?php return [ 'mysql' => [ [ 'host' => '127.0.0.1', // 服务器地址 'name' => 'dbname', // 数据库名 'user' => 'dbroot', // 用户名 'pwd' => 'dbpassword', // 密 'prefix' => 'dh_', // 数据库表前缀 'port' => '3309', // 端口 'dsn' => '', 'params' => '', 'charset' => 'utf8mb4', 'read_write_power' => 0, // 读写权限 0读写 1只写 2只读 'error_log' => true, // 是否开启错误Sql保存 'sql_explain' => true, // 开启sql性能查询 ], ], ]; ``` ### 数据库读写分离配置 >通过配置多个数据库信息 调整·read_write_power·值实现读写分离操作 ``` <?php return [ 'mysql' => [ [ ... 'read_write_power' => 1, // 读写权限 0读写 1只写 2只读 ... ], [ ... 'read_write_power' => 2, // 读写权限 0读写 1只写 2只读 ... ], ], ]; ``` ## **数据库操作** ### **创建数据库链接** >调用数据库操作类 先引用类库 `use denha\Db;` 执行方法 `Db::table('user')` 或者执行方法 `Db::table('dh_user',['is_prefix'=>false])` 或者执行 `Db::connection()->table('user')` 最终以上方式都可以链接数据表 `表前缀user` ### **链接指定数据库信息** `Db::connection($type,$config)->table('user')` `(string)type`指定名称 `(array)$config` 数据库配置信息 type类型值 | config值 | --- |--| |mysql|详见#配置内容 |sqlite| #### ** ### **where 条件筛选操作** `where($where, $value = null, $exp = null, $mapLink = 'AND')` #### 筛选类型 | 类型 | 值 | 例:普通 | 例:数组 | | --- | --- | -- | -- | | > | > | where('id','>',string) | $map['id] = ['>',string] | | < | < | where('id','<',string) | $map['id] = ['<',string] | | >= | >= | where('id','>=',string) | $map['id] = ['>=',string] | | <= | <= | where('id','<=',string) | $map['id] = ['<=',string] | | != | != | where('id','!=',string) | $map['id] = ['!=',string] | | <> | <> | where('id','<>',string) | $map['id] = ['<>',string] | | = | = | where('id','=',string) or where('id',string)| $map['id] = ['=',string] or $map['id] = string| | like | like | where('id','like','%string%') | $map['id] = ['like','%string%'] | | instr| instr|where('id','instr',string) | $map['id] = ['instr',string] | | locate| instr|where('id','locate',string) | $map['id] = ['local',string] | | in | in | where('id','in',array) | $map['id] = ['in',array] | | not in| not in | where('id','not in',array) | $map['id] = ['not in',array] | | not insrt| not insrt| where('id','not insrt',string) | $map['id] = ['not insrt',string] | | find_in_set| find_in_set| where('id','find_in_set',string) | $map['id] = ['find_in_set',string] | |or|or|where('id','or',['>=',string])|$map['id'] = ['or','id',array('>=',string) #### = 筛选 `Db::table('user')->where('id',1)->find()` 执行结果SQL:`SELECT * FROM 表前缀_user WHERE `id` = '1' LIMIT 1` #### in 筛选 `Db::table('user')->where('id','in',['1','2','3'])->select();`· 执行结果SQL:`SELECT * FROM 表前缀_user WHERE `id` in ('1','2','3')` #### between筛选 `Db::table('user')->where('created','between',['1000','2000'])->select()` 执行结果SQL:`SELECT * FROM 表前缀_user WHERE `created` between '1000' AND '2000'` #### 组合查询 `Db::table('user')->where('id',1)->where('status',1)->find()` 执行SQL:`SELECT * FROM 表前缀_user WHERE `id` = '1' AND `status` = '1' LIMIT 1` #### 数组组合查询 ``` $map = []; $map['id'] = 1; $map['status'] = ['in', '1,2']; $map['created'] = ['between', ['1000', '2000']]; table('user')->where($map)->select(); ``` 执行SQL:`SELECT * FROM 表前缀_user WHERE `id` = '1' AND `status` in ('1','2') AND `created` between '1000' AND '2000'` ### **Filed 字段查询操作** `Db::table('user')->field('id,name')->find();` 指定查询id和name ### **Join 链接操作** `Db::table('user as u')->join('表前缀_user_shop as ust','u.id = ust.uid','left')->select();` 连表查询 join必须指定全表名 ### **Limit 分页操作** 指定显示条数一般配合Select使用 Find 默认值显示一条 `Db::table('user')->where($map)->limit(25,25)->select();` ### **Group 分组操作** 合并展示操作 `Db::table('user')->where($map)->group('name')->select();` ### **Order 排序操作** #### 字符串模式 `Db::table('user')->order('id desc,status asc,rand()')->select();` #### 数组模式 `Db::table('user')->order(['id'=>'desc','status'=>'asc']->order('rand()')->select();` ### **Having 操作** `Db::table('user')->where($map)->group('name')->having('SUM(moeny) >= 100')->select();` ### **OR 操作** `Db::table('user')->where('id',int)->where('name','or',string)->where('tel','!=',string)->where('sex',int)->find();` ``` $sql = Select * from user where `id` = int ( and `name` = string or `tel` != stirng ) and `sex` = int limit 1 ``` ### **Query sql执行操作** ``` $sql = 'SELECT * FROM user WHERE `id` = 1'; ``` ### **Find 单条记录查询** ``` // 指定字段显示的两种写法 Db::table('user')->find('id,name'); Db::table('user')->field('id,name')->find(); ``` 指定字段查询一条记录 ### **Value 单条记录单个字段查询** `Db::table('user')->where($map)->value('id')` 返回 id值 ### **Lists单条记录数组返回** `Db::table('user')->where($map)->lists('id,name')` 返回 ['id','name']; 配合list() 使用直接赋值 `list($id,$name) = Db::table('user')->where($map)->lists('id,name');` 可以直接使用`$id` `$name` ### **Select 多条记录查询** `Db::table('user')->where($map)->select()` 查询多条记录值 ### **Column 多条记录单个字段查询** ``` 如果field是单个字段 则返回一位数组 Db::table('user')->where($map)->column('id') 返回数据 ['1','2','3'...] 如果field是两个字段 则第一个字段为value 第二个字段为key Db::table('user')->where($map)->column('name,id') 返回数据 ['1'=>'abc','2'=>'abc','3'=>'abcde'...] 如果field是两个字段以上 最后一个字段作为key 所有字段作为key的一维数组 需要注意key值重复的情况下按按顺行覆盖同名key Db::table('user')->where($map)->column('id,sex,name') 返回数据 ['abc'=>['id'=>1,'name'=>'abc','sex'=>0],'abcde'=>['id'=>3,'name'=>'abcde','sex'=>1]...] 如果需要返回所有数据并指定key值参考上一条语句 Db::table('user')->where($map)->column('*,name') 返回数据 ['abc'=>['id'=>1,'name'=>'abc','sex'=>0],'abcde'=>['id'=>3,'name'=>'abcde','sex'=>1]...] ``` ### **Count 获取记录条数** `Db::table('user')->where($map)->count();` 获取指定表和查询条件的所有记录 ### **Add 插入新数据记录** ``` $data = []; $data['name'] = 111; $data['money'] = 0; $data['status'] = 1; Db::table('user')->add($data); ``` >添加一条新记录记录值 ### **Save 更新数据记录** #### 单个字段更新 `Db::table('user')->where($map)->save('name','abc');` 更新查询调整修改记录的name为abc #### 普通多字段更新 ``` $data = []; $data['name'] = 111; $data['money'] = ['less',1]; $data['status'] = 1; Db::table('user')->where($map)->save($data); ``` #### 自增更新 `Db::table('user')->where($map)->save('money','add',int);` >用户金额自增N值 n=数字 #### 自减更新 `Db::table('user')->where($map)->save('money','less',int);` ### **Type 指定字段值类型** `type($name,$type)` #### type类型参数 | 类型 | 值 | 备注 | --- | --- | -- | -- | |int|PDO::PARAM_INT | 数字类型 |str|PDO::PARAM_STR | 字符串类型 |null|PDO::PARAM_NULL| null类型 |bool|PDO::PARAM_BOOL|布尔类型 |lob|PDO::PARAM_LOB|大数据类型 >默认是 PDO::PARAM_STR 字符串类型 例子 ``` Db::table('user')->where(['status'=>1])->find(); // 当前Sql: SELECT * from 表前缀user where status= '1' limit 1 查询的时候status是查询的字符串 // 如果status要做数字类型查询 Db::table('user')->where(['status'=>1])->type('status','int')->find(); // 或者 Db::table('user')->where(['status'=>1])->type(['status'=>'int'])->find(); // 当前Sql: SELECT * from 表前缀user where status= 1 limit 1 查询的时候status是查询的字符串 // in查询的时候每个参数指定类型 Db::table('user')->where('status','in',[,1,'abc',0,0])->find(); // 当前Sql: SELECT * from 表前缀user where status in('1','abc','0','0') limit 1 查询的时候status是查询的字符串 Db::table('user')->where('status','in',[,1,'abc',0])->type('status',['int','str','bool'])->find(); // 当前Sql: SELECT * from 表前缀user where status in('1','abc',false,'0') limit 1 查询的时候status是查询的字符串 // 当in指定参数是4个 type指定了3个参数 后续未指定参数继续使用str表示 // 当in指定参数是4个 type指定了1个参数 所有参数使用同一个类型 ```` ### **Delete 删除数据记录** `Db::table('user')->where($map)->delete();` >删除指定查询条件记录 >delete操作必须指定查询条件 防止整表删除 ### **子查询** `Db::childTable(Db::table('dingtalk_attendance')->where('work_id','>’,1)->field('work_id')->getSql())->where('work_id',1)->count()` 得到SQL:select count(*) as t from (select work_id from xx_dingtalk_attendance where work_id > 1) ### **事务操作** #### 开始事务 `Db::table('user')->startTrans();` #### 事务回滚 `Db::table('user')->rollback();` #### 事务提交 `Db::table('user')->commit();` ### **获取Sql语句** ``` Db::table('user')->where('id',1)->find(); Db::getLastSql(); ``` `Db::getLastSql();` 会返回上次执行的Sql语句