PHPackages                             qfz9527/think-orm - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. qfz9527/think-orm

ActiveLibrary[Database &amp; ORM](/categories/database)

qfz9527/think-orm
=================

think orm

3.0.x-dev(1y ago)06Apache-2.0PHPPHP &gt;=8.0.0

Since Jul 14Pushed 1y agoCompare

[ Source](https://github.com/qfz9527/think-orm)[ Packagist](https://packagist.org/packages/qfz9527/think-orm)[ RSS](/packages/qfz9527-think-orm/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (4)Used By (0)

think-orm
=========

[](#think-orm)

基于PHP5.6+ 的ORM实现，主要特性：

- 基于ThinkPHP5.1的ORM独立封装
- 支持Mysql、Pgsql、Sqlite、SqlServer、Oracle和Mongodb
- 支持Db类和查询构造器
- 支持事务
- 支持模型和关联

适用于不使用ThinkPHP框架的开发者。

安装

```
composer require topthink/think-orm

```

Db类用法：

```
use think\Db;
// 数据库配置信息设置（全局有效）
Db::setConfig(['数据库配置参数（数组）']);
// 进行CURD操作
Db::table('user')
	->data(['name'=>'thinkphp','email'=>'thinkphp@qq.com'])
	->insert();
Db::table('user')->find();
Db::table('user')
	->where('id','>',10)
	->order('id','desc')
	->limit(10)
	->select();
Db::table('user')
	->where('id',10)
	->update(['name'=>'test']);
Db::table('user')
	->where('id',10)
	->delete();
```

Db类增加的（静态）方法包括：

- `setConfig` 设置全局配置信息
- `getConfig` 获取数据库配置信息
- `setQuery` 设置数据库Query类名称
- `setCacheHandler` 设置缓存对象Handler（必须支持get、set及rm方法）
- `getSqlLog` 用于获取当前请求的SQL日志信息（包含连接信息）

其它操作参考TP5.1的完全开发手册[数据库](https://www.kancloud.cn/manual/thinkphp5_1/353998)章节

定义模型：

```
namespace app\index\model;
use think\Model;
class User extends Model
{
}
```

代码调用：

```
use app\index\model\User;

$user = User::get(1);
$user->name = 'thinkphp';
$user->save();
```

Db类和模型对比使用
----------

[](#db类和模型对比使用)

#### ✅ 创建Create

[](#white_check_mark---创建create)

- Db用法

    ```
    Db::table('user')
        ->insert([
            'name'  => 'thinkphp',
            'email' => 'thinkphp@qq.com',
        ]);
    ```
- 模型用法

    ```
    $user        = new User;
    $user->name  = 'thinkphp';
    $user->email = 'thinkphp@qq.com';
    $user->save();
    ```
- 或者批量设置

    ```
    $user = new User;
    $user->save([
        'name'  => 'thinkphp',
        'email' => 'thinkphp@qq.com',
    ]);
    ```

#### ✅ 读取Read

[](#white_check_mark--读取read)

- Db用法

    ```
    $user = Db::table('user')
        ->where('id', 1)
        ->find();
    //  或者
    $user = Db::table('user')
        ->find(1);
    echo $user['id'];
    echo $user['name'];
    ```
- 模型用法

    ```
    $user = User::get(1);
    echo $user->id;
    echo $user->name;
    ```
- 模型实现读取多个记录

    ```
    // 查询用户数据集
    $users = User::where('id', '>', 1)
        ->limit(5)
        ->select();

    // 遍历读取用户数据
    foreach ($users as $user) {
        echo $user->id;
        echo $user->name;
    }
    ```

#### ✅ 更新Update

[](#white_check_mark--更新update)

- Db用法

    ```
    Db::table('user')
        ->where('id', 1)
        ->update([
            'name'  => 'topthink',
            'email' => 'topthink@qq.com',
        ]);
    ```
- 模型用法

    ```
    $user        = User::get(1);
    $user->name  = 'topthink';
    $user->email = 'topthink@qq.com';
    $user->save();
    ```
- 或者使用

    ```
    $user = User::get(1);
    $user->save([
        'name'  => 'topthink',
        'email' => 'topthink@qq.com',
    ]);
    ```
- 静态调用

    ```
    User::update([
        'name'  => 'topthink',
        'email' => 'topthink@qq.com',
    ], ['id' => 1]);
    ```

#### ✅ 删除Delete

[](#white_check_mark--删除delete)

- Db用法

    ```
    Db::table('user')->delete(1);
    ```
- 模型用法

    ```
    $user = User::get(1);
    $user->delete();
    ```
- 或者静态实现

    ```
    User::destroy(1);
    ```
- 静态调用

    ```
    User::update([
        'name'  => 'topthink',
        'email' => 'topthink@qq.com',
    ], ['id' => 1]);
    ```
- destroy方法支持删除指定主键或者查询条件的数据

    ```
    // 根据主键删除多个数据
    User::destroy([1, 2, 3]);
    // 指定条件删除数据
    User::destroy([
        'status' => 0,
    ]);
    // 使用闭包条件
    User::destroy(function ($query) {
        $query->where('id', '>', 0)
            ->where('status', 0);
    });
    ```

更多模型用法可以参考5.1完全开发手册的[模型](https://www.kancloud.cn/manual/thinkphp5_1/354041)章节

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~258 days

Total

2

Last Release

649d ago

Major Versions

2.0.x-dev → 3.0.x-dev2024-07-29

PHP version history (2 changes)2.0.x-devPHP &gt;=7.1.0

3.0.x-devPHP &gt;=8.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/605889c14b71a45f23c0853ddb6cbbb6a24243cfd7d37244ddc3739b738ee7a3?d=identicon)[qfz9527](/maintainers/qfz9527)

---

Top Contributors

[![liu21st](https://avatars.githubusercontent.com/u/1111670?v=4)](https://github.com/liu21st "liu21st (125 commits)")[![evalor](https://avatars.githubusercontent.com/u/26944445?v=4)](https://github.com/evalor "evalor (2 commits)")[![Tinywan](https://avatars.githubusercontent.com/u/14959876?v=4)](https://github.com/Tinywan "Tinywan (2 commits)")[![big-dream](https://avatars.githubusercontent.com/u/9215157?v=4)](https://github.com/big-dream "big-dream (2 commits)")[![kphcdr](https://avatars.githubusercontent.com/u/3817838?v=4)](https://github.com/kphcdr "kphcdr (1 commits)")[![pppscn](https://avatars.githubusercontent.com/u/5105854?v=4)](https://github.com/pppscn "pppscn (1 commits)")[![shaukei](https://avatars.githubusercontent.com/u/1581783?v=4)](https://github.com/shaukei "shaukei (1 commits)")[![willove](https://avatars.githubusercontent.com/u/5616317?v=4)](https://github.com/willove "willove (1 commits)")[![iakihsoug](https://avatars.githubusercontent.com/u/5564630?v=4)](https://github.com/iakihsoug "iakihsoug (1 commits)")[![Junjunya1995](https://avatars.githubusercontent.com/u/24283128?v=4)](https://github.com/Junjunya1995 "Junjunya1995 (1 commits)")[![klinson](https://avatars.githubusercontent.com/u/41332835?v=4)](https://github.com/klinson "klinson (1 commits)")

---

Tags

databaseorm

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/qfz9527-think-orm/health.svg)

```
[![Health](https://phpackages.com/badges/qfz9527-think-orm/health.svg)](https://phpackages.com/packages/qfz9527-think-orm)
```

###  Alternatives

[topthink/think-orm

the PHP Database&amp;ORM Framework

4542.0M217](/packages/topthink-think-orm)[cycle/database

DBAL, schema introspection, migration and pagination

64690.9k31](/packages/cycle-database)[tommyknocker/pdo-database-class

Framework-agnostic PHP database library with unified API for MySQL, MariaDB, PostgreSQL, SQLite, MSSQL, and Oracle. Query Builder, caching, sharding, window functions, CTEs, JSON, migrations, ActiveRecord, CLI tools, AI-powered analysis. Zero external dependencies.

845.7k](/packages/tommyknocker-pdo-database-class)[perplorm/perpl

Perpl is an improved and still maintained fork of Propel2, an open-source Object-Relational Mapping (ORM) for PHP.

203.7k](/packages/perplorm-perpl)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
