【ThinkPHP6.x框架】(8)数据库进阶4(事务与结果集处理)

事务处理

        1. 数据库的表引擎需要是  InnoDB才可以使用,如果不是调整即可;
        2. 事务处理,需要执行多个  SQL查询,数据是关联恒定的;
        3. 如果成功一条查询,改变了数据,而后一条失败,则前面的数据回滚;
        4. 比如:蜡笔小新给路飞  3快钱,自己-3,对方+3,这时需要事务处理;
        5. 系统提供了两种事务处理的方式,第一种是自动处理,出错自动回滚;

Db::transaction(function () {
Db::name('user')->where('id', 19)->save(['price'=>Db::raw('price - 3')]); 
Db::name('user1')->where('id', 20)->save(['price'=>Db::raw('price + 3')]);
});

        6. 手动处理,基本和原生处理类似,可以自行输出错误信息;

//启动事务
Db::startTrans(); 
try {
    Db::name('user')->where('id', 19)->save(['price'=>Db::raw('price - 3')]);       
    Db::name('user1')->where('id', 20)->save(['price'=>Db::raw('price + 3')]); 
    //提交事务 
    Db::commit();
} catch (\Exception $e) { 
    echo '执行SQL失败!'; //回滚
    Db::rollback(); 
}

获取器

        1. 获取器的意思就是:将数据的字段进行转换处理再进行操作;
        2. 比如在获取数据列表的时候,将获取到的邮箱字段全部大写;其中,第一个参数为该字段的值('email');第二个参数为查询的全部结果。

$user = Db::name('user')->withAttr('email', function ($value, $data) { 
    return strtoupper($value);
})->select(); 

return json($user);

代码提示与数据集

一.代码提示
        1. 一般来说,代码提示是通过注释来告知编辑器自动补全方法等;
        2. 对比一下,5.1的类库,发现6.x的类库没有写注释;
        3. 就拿Db类来说,只要把  5.1的注释覆盖到  6.x即可实现代码提示;
        4. 当然,是否精确,需要一一对比这些方法和参数,但绝大多数是相同的;
        5. 找到  Db类,看它的注释:@see指向的是谁,是:DbManager类;
        6. 然后将  5.1的Db类注释,复制给  DbManager类即可,以后其它的雷同操作;

        在tp6中,DbManager的目录为vendor/topthink/framework/src/think/Db.php,注释如下:

* Class DbManager
* @package think
* @mixin BaseQuery
* @mixin Query
* @method \think\db\Query master() static 从主服务器读取数据
* @method \think\db\Query readMaster(bool $all = false) static 后续从主服务器读取数据
* @method \think\db\Query table(string $table) static 指定数据表(含前缀)
* @method \think\db\Query name(string $name) static 指定数据表(不含前缀)
* @method \think\db\Query where(mixed $field, string $op = null, mixed $condition = null) static 查询条件
* @method \think\db\Query whereRaw(string $where, array $bind = []) static 表达式查询
* @method \think\db\Query whereExp(string $field, string $condition, array $bind = []) static 字段表达式查询
* @method \think\db\Query when(mixed $condition, mixed $query, mixed $otherwise = null) static 条件查询
* @method \think\db\Query join(mixed $join, mixed $condition = null, string $type = 'INNER') static JOIN查询
* @method \think\db\Query view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询
* @method \think\db\Query field(mixed $field, boolean $except = false) static 指定查询字段
* @method \think\db\Query fieldRaw(string $field, array $bind = []) static 指定查询字段
* @method \think\db\Query union(mixed $union, boolean $all = false) static UNION查询
* @method \think\db\Query limit(mixed $offset, integer $length = null) static 查询LIMIT
* @method \think\db\Query order(mixed $field, string $order = null) static 查询ORDER
* @method \think\db\Query orderRaw(string $field, array $bind = []) static 查询ORDER
* @method \think\db\Query cache(mixed $key = null , integer $expire = null) static 设置查询缓存
* @method \think\db\Query withAttr(string $name,callable $callback = null) static 使用获取器获取数据
* @method mixed value(string $field) static 获取某个字段的值
* @method array column(string $field, string $key = '') static 获取某个列的值
* @method mixed find(mixed $data = null) static 查询单个记录
* @method mixed select(mixed $data = null) static 查询多个记录
* @method integer insert(array $data, boolean $replace = false, boolean $getLastInsID = false, string $sequence = null) static 插入一条记录
* @method integer insertGetId(array $data, boolean $replace = false, string $sequence = null) static 插入一条记录并返回自增ID
* @method integer insertAll(array $dataSet) static 插入多条记录
* @method integer update(array $data) static 更新记录
* @method integer delete(mixed $data = null) static 删除记录
* @method boolean chunk(integer $count, callable $callback, string $column = null) static 分块获取数据
* @method \Generator cursor(mixed $data = null) static 使用游标查找记录
* @method mixed query(string $sql, array $bind = [], boolean $master = false, bool $pdo = false) static SQL查询
* @method integer execute(string $sql, array $bind = [], boolean $fetch = false, boolean $getLastInsID = false, string $sequence = null) static SQL执行
* @method \think\Paginator paginate(integer $listRows = 15, mixed $simple = null, array $config = []) static 分页查询
* @method mixed transaction(callable $callback) static 执行数据库事务
* @method void startTrans() static 启动事务
* @method void commit() static 用于非自动提交状态下面的查询提交
* @method void rollback() static 事务回滚
* @method boolean batchQuery(array $sqlArray) static 批处理执行SQL语句
* @method string getLastInsID(string $sequence = null) static 获取最近插入的ID

        在tp6中,Model的目录为vendor/topthink/think-orm/src/Model.php,注释如下:

* @method Query where(mixed $field, string $op = null, mixed $condition = null) static 查询条件
* @method Query whereTime(string $field, string $op, mixed $range = null) static 查询日期和时间
* @method Query whereBetweenTime(string $field, mixed $startTime, mixed $endTime) static 查询日期或者时间范围
* @method Query whereBetweenTimeField(string $startField, string $endField) static 查询当前时间在两个时间字段范围
* @method Query whereYear(string $field, string $year = 'this year') static 查询某年
* @method Query whereMonth(string $field, string $month = 'this month') static 查询某月
* @method Query whereDay(string $field, string $day = 'today') static 查询某日
* @method Query whereRaw(string $where, array $bind = []) static 表达式查询
* @method Query whereExp(string $field, string $condition, array $bind = []) static 字段表达式查询
* @method Query when(mixed $condition, mixed $query, mixed $otherwise = null) static 条件查询
* @method Query join(mixed $join, mixed $condition = null, string $type = 'INNER') static JOIN查询
* @method Query view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询
* @method Query with(mixed $with) static 关联预载入
* @method Query count(string $field) static Count统计查询
* @method Query min(string $field) static Min统计查询
* @method Query max(string $field) static Max统计查询
* @method Query sum(string $field) static SUM统计查询
* @method Query avg(string $field) static Avg统计查询
* @method Query field(mixed $field, boolean $except = false) static 指定查询字段
* @method Query fieldRaw(string $field, array $bind = []) static 指定查询字段
* @method Query union(mixed $union, boolean $all = false) static UNION查询
* @method Query limit(mixed $offset, integer $length = null) static 查询LIMIT
* @method Query order(mixed $field, string $order = null) static 查询ORDER
* @method Query orderRaw(string $field, array $bind = []) static 查询ORDER
* @method Query cache(mixed $key = null, integer $expire = null) static 设置查询缓存
* @method mixed value(string $field) static 获取某个字段的值
* @method array column(string $field, string $key = '') static 获取某个列的值
* @method Model find(mixed $data = null) static 查询单个记录 不存在返回Null
* @method Model findOrEmpty(mixed $data = null) static 查询单个记录 不存在返回空模型
* @method \think\model\Collection select(mixed $data = null) static 查询多个记录
* @method Model withAttr(array $name, \Closure $closure) 动态定义获取器

二.数据集
        1. 所谓数据集,是当查询后的结果集,它是  think\Collection类型和数组一样;
        2. 虽然操作和数组类似,但它额外提供了一些方法,方法如下:

        3. 注意:这里的方法关键字某些和数据库查询类似,但它是数据集操作的方法;
        4. 由于方法较多,我们尝试几个理解一下即可,有需要可以回头查阅;

//获取数据集
$user = Db::name('user')->select(); 
//转换成数组
var_dump($user->toArray()); 
//将数据集随机打乱
dump($user->shuffle()); 
//删掉数据中最后一个元素 $user->pop();
dump($user);
//使用whereIn查询结果集
dump($user->whereIn('id', [19,20,21]));

        5. 每种的典型,都拎出来说明了一下;
        6. 其它的方法,可以通过追逐方法查阅注释和参数来参考使用。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值