简单快速的“ORM”框架
v1.0.3
版本
2024-11-13
版本更新时间
4
安装
2
star
简介
一个简单、快速的基于PHP和PDO的数据库中间层
感觉使用Laravel ORM和ThinkORM麻烦的可以试试这个
安装
composer require tomener/svickorm
安装后将自动生成 config/svickorm.php 数据库配置文件,开发者需要根据实际情况手动更改数据库配置。
数据库
<?php
namespace app\controller;
use support\Request;
use Svickorm\Db;
class Index
{
public function db(Request $request)
{
$id = $request->getInt('id');
$p = $request->getInt('p', 1);
//查询所有字段
$user = Db::table('user')->where($id)->row();
//查询指定字段
$user = Db::table('user')->where($id)->row('id, nickname');
//查询列表
$page = ['limit' => 10, 'p' => $p];
$users = Db::table('user')->where(['status' => 1, 'level' => ['>', 1]])
->select('id, nickname')
->page($page)
->orderBy('level DESC')
->list();
return result(0, 'OK', [
'info' => $user,
'list' => $users,
'page' => $page,
]);
}
}
模型
模型文件
模型文件可以放在/app/model下面,也可以单独创建一个user文件夹,放在user文件夹下面,svickorm模型继承Svickorm\Model,类似如下
<?php
namespace app\model\user;
use Svickorm\Model;
class User extends Model
{
}
使用模型
<?php
namespace app\controller;
use app\model\user\User;
use support\Request;
use Svickorm\Db;
class Index
{
public function list(Request $request)
{
$id = $request->getInt('id');
$p = $request->getInt('p', 1);
//查询所有字段
$user = User::where($id)->row();
// 或者
$user = User::find($id);
//查询指定字段
$user = User::where($id)->row('id, nickname');
//或者
$user = User::find($id, 'id, nickname');
//查询列表
$page = ['limit' => 10, 'p' => $p];
$users = User::where(['status' => 1, 'level' => ['>', 2]])
->select('id, nickname')
->page($page)
->orderBy('level DESC')
->list();
return result(0, 'OK', [
'info' => $user,
'list' => $users,
'page' => $page,
]);
}
}
查询一列
$user_ids = [1, 5, 8];
$user_map = User::where(['id' = ['in', $user_ids]])->column('name');
/**
['lucy', 'candy', 'tommy']
*
$user_map = User::where(['id' = ['in', $user_ids]])->column('name', 'id');
//输出
/**
[
[1] => 'lucy',
[5] => 'candy',
[8] => 'tommy',
]
*/
$user_map = User::where(['id' = ['in', $user_ids]])->column('name, level', 'id');
//输出
/**
[
[1] => ['name' => 'lucy', 'level' => 2],
[5] => ['name' => 'candy', 'level' => 1],
[8] => ['name' => 'tommy', 'level' => 5],
]
*/
获取字段值
$id = 5;
$name = User::where($id)->value('name');
插入数据
User::insert(['name' => 'bob', 'level' => 2]);
User::insertAll([
['name' => 'bob', 'level' => 2],
['name' => 'weky', 'level' => 1]
]);
更新数据
$uid = 100;
User::where($uid)->update([
'level' => 5,
'nickname' => 'womeen',
'score' => ['+', 5]
]);
User::where($uid)->inc('score');
User::where($uid)->dec('score', 5);
统计(sum count avg max min)
$where = ['level' => 2, 'status' => 1];
User::where($where)->sum('money');
User::where($where)->count();
User::where($where)->avg('score');
User::where($where)->max('score');
User::where($where)->min('score');
删除
$uid = 100;
User::where($uid)->delete();
连表查询
$page = ['limit' => 10, 'p' => 1];
$admins = Db::table('admin')->alias('a')
->join('user u', 'a.uid = u.id')
->select('a.id, a.uid, a.name, u.nickname')
->where('a.status', 1)
->where(['u.level' => 2, 'u.status' => 1])
->page($page)
->list()
事务
//开启事务
User::connection()->startTrans();
//回滚事务
User::connection()->rollback();
//提交事务
User::connection()->commit();
获取最后一条SQL
User::connection()->getLastSql();
检测连接是否可用
User::connection()->ping()
设置排它锁(X锁)
$id = 100;
$num = 2;
User::where(['id' => $id, 'stock' => ['>=', $num]])
->forUpdate()
->update([
'stock' => ['-', $num]
]);
更多方法请参考 使用文档