一个简单、快速的基于PHP和PDO的数据库中间层
使用Laravel ORM和ThinkORM,感觉不好用,把Svick框架里面的ORM抽离出来搞了一个
https://gitee.com/tomener/svickorm
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(['level' => 2])
->orderBy('sort DESC')
->row();
$user = Db::table('user')->find($id);
//查询指定字段
$user = Db::table('user')->where($id)->row('id, nickname');
$user = Db::table('user')->find($id, '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::find($id)->toArray();
//查询指定字段
$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
$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();
User::connection()->getLastSql();
User::connection()->ping()
$id = 100;
$num = 2;
User::where(['id' => $id, 'stock' => ['>=', $num]])
->forUpdate()
->update([
'stock' => ['-', $num]
]);
更多方法请参考 使用文档
这个方法是自己扩展的吗,自动转 int ?
是的,在support\Request里面扩展的
/**
*/
public function getInt($name = null, int $default = 0): int
{
return intval($this->get($name, $default));
}
明白了,感谢回答
这个还是推荐使用
Laravel ORM
或者ThinkORM
,自己搞一套维护成本太大了适合自己就行,用了几年了没啥问题