tomener/svickorm

SvickORM is a simple and fast ORM framework.

v1.0.3 2024-11-13 04:13 UTC

This package is auto-updated.

Last update: 2024-12-13 04:18:06 UTC


README

一个简单、快速的“ORM”框架。

快速上手

1. 安装

composer require tomener/svickorm

2. 配置

在/config目录中新增svickorm.php文件,配置数据库连接信息。

return [
    'mysql' => [
        'default' => [
            'host' => 'localhost',
            'port' => 3306,
            'database' => 'test',
            'charset' => 'utf8',
            'user' => 'root',
            'password' => 123456,
            'prefix' => '',
        ],
    ],
];

3. 创建模型

在/app/model/user目录下创建User.php文件

<?php

namespace app\model\user;


use Svickorm\Model;

class User extends Model
{

}

4. 使用

在控制器中使用

<?php

namespace app\controller;
use app\model\user\User;
use support\Request;
use support\Response;

class User
{
    public function info(Request $request)
    {
        $id = $request->get('id');
        
        //$user = User::find($id);
        $user = User::find($id, 'id, nickname');
        //$user = User::where(['status' => 1, 'level' => 2'])->row();

        return result(0, 'OK', [
            'info' => $user,
        ]);
    }
    
    public function list(Request $request)
    {
        $p = $request->get('p', 1);

        $page = ['limit' => 10, 'p' => $p];

        $users = User::where(['status' => 1, 'level' => ['>', 2]])
            ->select('id, nickname, avatar')
            ->orderBy('id DESC')
            ->page($page)
            ->list();

        return result(0, 'OK', [
            'list' => $users,
            'page' => $page,
        ]);
    }
}

说明: 其中用到的result函数是在/support/helper.php中定义的。

/**
 * Json response
 * @param int $code
 * @param string $msg
 * @param null $data
 * @return Response
 */
function result(int $code = 0, string $msg = 'OK', $data = null): Response
{
    $ret = [
        'code' => $code,
        'msg' => $msg,
    ];
    if ($data !== null) {
        $ret['data'] = $data;
    }
    return new Response(200, ['Content-Type' => 'application/json'], json_encode($ret, JSON_UNESCAPED_UNICODE));
}

使用文档

更多方法请参考 使用文档