thinkphp db、cache助手函数

62 阅读1分钟
<?php
namespace app\index\controller;
use tink\Db;


class Index
{

    public function hello()
    {
        
        
       // ->value("user_name"); ->where('uid',1)  find  select  column("user_name","byname","uid");
       //->field('user_name,byname,uid,sum(uid) a')
       //->with('department')

        
        $where['dept_id']  = ['>',6];
        $where['uid']  = ['>',10];

        
        $r['data']=db('user')->where($where)->field('USER_NAME,dept_id')->page(input('page'),5)->select();
        
        
        $r['total']=db('user')->count();
        
       //输出最后执行的sql,方便调试
        echo db('user')->getLastSql();
        
        try {

            // 开启事务

            db()->startTrans();

            // 执行SQL语句

            db()->execute("UPDATE `accounts` SET `balance` = balance - 100 WHERE `user_id` = 1");

            db()->execute("UPDATE `accounts` SET `balance` = balance + 100 WHERE `user_id` = 2");

            // 提交事务

            db()->commit();

            } catch (\Exception $e) {

            // 回滚事务

            db()->rollback();

            // 处理异常

            throw $e;

            }
        

    }
    
    public function c(){
        
        // 设置缓存数据
        cache('aa', "adsd", 3600);
        // 获取缓存数据
        var_dump(cache('aa'));
        // 删除缓存数据
        cache('aa', NULL);
    }
    
}
`