hello,word

fastadmin数据库操作

1.model操作

如题,使用命令行工具生成数据库表对应的model,通过model进行数据库操作,操作方式有些像yii2框架

例如

//插入或者更新

$user =  new Users();

$user->username = 'zhangsan';

$user->password = md5("123456"+$salt);

$user->save();

//查询

$users = Users::where(['status'=>0])->find();//拿一条

$users = Users::where(['status'=>0])->select();//拿多条

$users = Users::where(['status'=>0])->select();//拿多条

$users = Users::where(['status'=>0])->paginate($limit,['page'=>$page]);//分页

2.db 原生sql查询

use think\Db;
Db::query("select * from user where id = :id",[“:id”=$id]);//查询
Db::execute("update user set username = :username where id = :id",[':username'=>$username,':id'=>$id]);//非查询sql
3.db的函数方法
 * @method static Query name(string $name) 指定数据表(不含前缀)
 * @method static Query where(mixed $field, string $op = null, mixed $condition = null) 查询条件
 * @method static Query join(mixed $join, mixed $condition = null, string $type = 'INNER') JOIN查询
 * @method static Query union(mixed $union, boolean $all = false) UNION查询
 * @method static Query limit(mixed $offset, integer $length = null) 查询LIMIT
 * @method static Query order(mixed $field, string $order = null) 查询ORDER
 * @method static Query cache(mixed $key = null , integer $expire = null) 设置查询缓存
 * @method static mixed value(string $field) 获取某个字段的值
 * @method static array column(string $field, string $key = '') 获取某个列的值
 * @method static Query view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') 视图查询
 * @method static mixed find(mixed $data = null) 查询单个记录
 * @method static mixed select(mixed $data = null) 查询多个记录
 * @method static integer insert(array $data, boolean $replace = false, boolean $getLastInsID = false, string $sequence = null) 插入一条记录
 * @method static integer insertGetId(array $data, boolean $replace = false, string $sequence = null) 插入一条记录并返回自增ID
 * @method static integer insertAll(array $dataSet) 插入多条记录
 * @method static integer update(array $data) 更新记录
 * @method static integer delete(mixed $data = null) 删除记录
//例如
 $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
 
 
posted @ 2025-07-14 18:24  tying  阅读(127)  评论(0)    收藏  举报