think-orm插件

v1.1.1 版本
2023-04-23 版本更新时间
19038 安装
7 star

简介

think-orm是ThinkPHP官方的一个基于PHP和PDO的数据库中间层和ORM类库。

webman/think-orm 是一个自动化安装 topthink/think-orm 的插件。它做了三个事情
1、安装ThinkPHP官方的原生topthink/think-orm组件
2、webman项目里自动增加配置文件config/thinkorm.php
3、设置定时器定时向数据库发送select 1语句,避免数据库连接超时间空闲被数据库服务端断开。

安装

composer require -W webman/think-orm

安装后将自动生成 config/thinkorm.php 数据库配置文件,开发者需要根据实际情况手动更改数据库配置。

提示
如果出现无法安装,可能是由于使用了composer代理导致,可运行命令composer config -g --unset repos.packagist去掉代理再次尝试。

使用

<?php
namespace app\controller;

use support\Request;
use think\facade\Db;

class Foo
{
  public function get(Request $request)
  {
      $user = Db::table('user')->where('uid', '>', 1)->find();
      return json($user);
  }
}

模型

ThinkOrm模型继承think\Model,类似如下

<?php
namespace app\model;

use think\Model;

class User extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $pk = 'id';

}

你也使用以下命令创建基于thinkorm的模型

php webman make:model 表名

提示
此命令需要安装webman/console,安装命令为composer require webman/console ^1.2.13

注意
make:model 命令如果检测到主项目使用了illuminate/database,会优先创建基于illuminate/database的模型文件,而不是thinkorm的,这时可以通过附加一个参数tp来强制生成think-orm的模型,命令类似 php webman make:model 表名 tp (如果不生效请升级webman/console)

更多请参考topthink/think-orm文档