ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
要使用Db类必须使用门面方式(`think\facade\Db`)调用助手函数在5.2被移除了 第二种方式是通过容器获取:`$db=app("db");` >[danger]新版的数据库和模型操作已经独立为[`ThinkORM`](https://github.com/top-think/think-orm)库,默认安装应用的时候会自动安装,如果你不需要使用该`ORM`库的话,可以单独卸载`topthink/think-orm`后安装其它的`ORM`库。 ~~~ Query基类类BaseQuery __call(string $method, array $args) 利用__call方法实现一些特殊的Model方法 newQuery(): BaseQuery 创建一个新的查询对象 getConnection():ConnectionInterface 获取当前的数据库Connection对象 name($name):this 指定默认的数据表名(不含前缀) getName(): string 获取当前的数据表名称 getConfig(string $name = ''):mixed 获取数据库的配置参数 getTable(string $name = ''):mixed 得到当前或者指定名称的数据表 setFieldType(array $type):this 设置字段类型信息 getLastSql(): string 获取最近一次查询的sql语句 getNumRows(): int 获取返回或者影响的记录数 getLastInsID(string $sequence = null) 获取最近插入的ID value(string $field, $default = null):mixed 得到某个字段的值 column(string $field, string $key = ''): array 得到某个列的数组 union($union, bool $all = false):this 查询SQL组装 union unionAll($union):this 查询SQL组装 union all field($field):this 指定查询字段 withoutField($field):this 指定要排除的查询字段 tableField($field, string $tableName, string $prefix = '', string $alias = ''):this 指定其它数据表的查询字段 data(array $data):this 设置数据 removeOption(string $option = ''):this 去除查询参数 limit(int $offset, int $length = null):this 指定查询数量 page(int $page, int $listRows = null): 指定分页 table($table):this 指定当前操作的数据表 order($field, string $order = ''):this 指定排序 order('id','desc') 或者 order(['id'=>'desc','create_time'=>'desc']) paginate($listRows = null, $simple = false): Paginator 分页查询 paginateX($listRows = null, string $key = null, string $sort = null): Paginator 根据数字类型字段进行分页查询(大数据) more(int $limit, $lastId = null, string $key = null, string $sort = null): array 根据最后ID查询更多N个数据 cache($key = true, $expire = null, $tag = null):this 查询缓存 lock($lock = false):this 指定查询lock alias($alias):this 指定数据表别名 master(bool $readMaster = true):this 设置从主服务器读取数据 strict(bool $strict = true):this 设置是否严格检查字段名 sequence(string $sequence = null):this 设置自增序列名 json(array $json = [], bool $assoc = false):this 设置JSON字段信息 pk($pk):this 指定数据表主键 getOptions(string $name = ''):mixed 获取当前的查询参数 setOption(string $option, $value):this 设置当前的查询参数 via(string $via = ''):this 设置当前字段添加的表别名 save(array $data = [], bool $forceInsert = false):integer 保存记录 自动判断insert或者update insert(array $data = [], bool $getLastInsID = false):integer|string 插入数据 参数2为true返回自增主键 insertGetId(array $data):integer|string 插入记录并获取自增ID insertAll(array $dataSet = [], int $limit = 0): int 批量插入记录 selectInsert(array $fields, string $table): int 通过Select方式插入记录 update(array $data = []): int 更新记录 delete($data = null): int 删除记录 select($data = null): Collection 查找记录 find($data = null):array|Model|null 查找单条记录 parseOptions(): array 分析表达式(可用于查询或者写入操作) parseUpdateData(&$data): bool 分析数据是否存在更新条件 parsePkWhere($data): void 把主键值转换为查询条件 支持复合主键 project权限 options(array $options):this 查询参数批量赋值 getModelUpdateCondition(array $options):?? 获取模型的更新条件 Query类 Query extends BaseQuery orderRaw($field, array $bind = []):this 表达式方式指定Field排序 fieldRaw(string $field):this 表达式方式指定查询字段 orderField(string $field, array $values, string $order = ''):this 指定Field排序 orderField('id',[1,2,3],'desc') orderRand():this 随机排序 exp(string $field, string $value):this 使用表达式设置数据 tableRaw(string $table):this 表达式方式指定当前操作的数据表 query(string $sql, array $bind = []): array 执行查询 返回数据集 execute(string $sql, array $bind = []): int 执行语句 fetchSql(bool $fetch = true):$this|Fetch 获取执行的SQL语句而不进行实际的查询 为true 返回sql batchQuery(array $sql = []): bool 批处理执行SQL语句(批处理的指令都认为是execute操作) using($using):this USING支持 用于多表删除 procedure(bool $procedure = true):this 存储过程调用 group($group):this 指定group查询 having(string $having):this 指定having查询 distinct(bool $distinct = true):this 指定distinct查询 force(string $force):this 指定强制索引 comment(string $comment):this 查询注释 replace(bool $replace = true):this 设置是否REPLACE partition($partition):this 设置当前查询所在的分区 duplicate($duplicate):this 设置DUPLICATE extra(string $extra):this 设置查询的额外参数 buildSql(bool $sub = true): string 创建子查询SQL getPk():string|array 获取当前数据表的主键 autoinc(string $autoinc):this 指定数据表自增主键 getAutoInc():string|null 获取当前数据表的自增主键 inc(string $field, float $step = 1):this 字段值增长 dec(string $field, float $step = 1):this 字段值减少 getQueryGuid($data = null): string 获取当前的查询标识 getPdo(): PDOStatement 执行查询但只返回PDOStatement对象 cursor($data = null):\Generator 使用游标查找记录 chunk(int $count, callable $callback, $column = null, string $order = 'asc'): bool 分批数据返回处理 ~~~ **BaseQuery 引用了如下trait类** use concern\TimeFieldQuery;//时间查询 use concern\AggregateQuery;//聚合查询 use concern\ModelRelationQuery;//模型及关联查询 use concern\ResultOperation;//查询数据处理 use concern\Transaction;//事务支持 use concern\WhereQuery;//条件支持 **Query引用了如下trait类** use concern\JoinAndViewQuery;//JOIN和VIEW查询 use concern\ParamsBind;//参数绑定 use concern\TableFieldInfo;//数据字段信息 ## **trait JoinAndViewQuery:JOIN和VIEW查询** \vendor\topthink\think-orm\src\db\concern\JoinAndViewQuery.php ``` join($join="关联的表名:", string $condition = 条件:null, string $type = 'JOIN类型:INNER', array $bind = 参数绑定:[]):this 查询SQL组装 join leftJoin($join, string $condition = null, array $bind = []) LEFT JOIN rightJoin($join, string $condition = null, array $bind = []) RIGHT JOIN fullJoin($join, string $condition = null, array $bind = []) FULL JOIN view($join="数据表:", $field = 查询字段:true, $on = JOIN条件:null, string $type = 'JOIN类型:INNER', array $bind = 参数绑定:[]) 指定JOIN查询字段 //示例: Db::view('User', 'id,name') ->view('Profile', 'truename,phone,email', 'Profile.user_id=User.id', 'LEFT') ->view('Score', 'score', 'Score.user_id=Profile.id', 'RIGHT') ->where('score', '>', 80) ->select(); //User.name,Profile.truename,Profile.phone,Profile.email,Score.score FROM think_user User LEFT JOIN think_profile Profile ON Profile.user_id=User.id RIGHT JOIN think_socre Score ON Score.user_id=Profile.id WHERE Score.score > 80 ```