简单快速的“ORM”框架,基于PHP和PDO的数据库中间层

ontheway

简介

一个简单、快速的基于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();

获取最后一条SQL

User::connection()->getLastSql();

检测连接是否可用

User::connection()->ping()

设置排它锁(X锁)

$id = 100;
$num = 2;
User::where(['id' => $id, 'stock' => ['>=', $num]])
    ->forUpdate()
    ->update([
        'stock' => ['-', $num]
    ]);

更多方法请参考 使用文档

331 2 0
2个评论

ersic
$request->getInt('id');

这个方法是自己扩展的吗,自动转 int ?

  • ontheway 29天前

    是的,在support\Request里面扩展的
    /**

    • @param $name
    • @param int $default
    • @return int
      */
      public function getInt($name = null, int $default = 0): int
      {
      return intval($this->get($name, $default));
      }
  • ersic 29天前

    明白了,感谢回答

Tinywan

这个还是推荐使用 Laravel ORM 或者 ThinkORM,自己搞一套维护成本太大了

  • ontheway 28天前

    适合自己就行,用了几年了没啥问题

ontheway

2715
积分
0
获赞数
0
粉丝数
2016-12-28 加入
×
🔝