PHPackages                             easy-swoole/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. [Framework](/categories/framework)
4. /
5. easy-swoole/orm

ActiveLibrary[Framework](/categories/framework)

easy-swoole/orm
===============

php stander lib

2.0.1(yesterday)02↑2900%Apache-2.0PHPPHP &gt;=8.1.0

Since Mar 23Pushed yesterdayCompare

[ Source](https://github.com/shengdoushi/easyswoole-orm)[ Packagist](https://packagist.org/packages/easy-swoole/orm)[ Docs](https://www.easyswoole.com/)[ RSS](/packages/easy-swoole-orm/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (6)Versions (2)Used By (0)

EASYSWOOLE ORM
==============

[](#easyswoole-orm)

A component for operate database for easyswoole

项目背景
----

[](#项目背景)

由于swoole协程环境不可以直接使用php-fpm的orm组件（由于存在静态全局变量、连接层没有做好协程处理，无法协程安全地使用）

所以easyswoole花费大量时间精力维护orm组件，连贯操作等功能设计借鉴TP5.0的ORM组件。

有疑问、功能建议、bug反馈请在QQ群、github issue、直接联系宣言提交。

问题反馈模板
------

[](#问题反馈模板)

一个完整的提问需要包含以下几点：

- 1.出现了问题，怀疑orm组件的bug，需要先行编写最小测试。（比如一个新的类，单独只调用一个功能，都出问题了，排除其他因素影响）
- 2.用文字描述出现的问题，附带运行和调试的参数截图
- 3.附带第一步最小测试复现脚本

安装
--

[](#安装)

```
composer require easy-swoole-php/orm

```

RFC
---

[](#rfc)

Model Invoke

### 查询

[](#查询)

#### 1.1、获取单个数据

[](#11获取单个数据)

```
// 主键条件
Model::invoke()->get(1);
=> select * from table where primary_key = 1

// 数组条件
Model::invoke()->get([col => val]);
// 闭包条件查询
Model::invoke()->get(function ($query) {
    $query->where(col, val);
    // Or $query->where([col => val]);
});
Model::invoke()->where(col, val)->get();
Model::invoke()->where(col, val, '=')->get();
Model::invoke()->where(col, val)->find();
=> select * from table where col = val limit 1
```

#### 1.2、获取多个数据

[](#12获取多个数据)

```
// 主键条件
Model::invoke()->all('1,2,3');
// 主键条件数组
Model::invoke()->all([1,2,3]);
=> select * from table where primary_key in (1,2,3)

// 数组条件
Model::invoke()->all([col => val]);
=> select * from table where col = val

// 闭包条件查询
Model::invoke()->all(function ($query) {
    $query->where(col, val)->limit(num)->order(col, 'asc')
});
// 链式调用
Model::invoke()->where(col, val)
    ->limit(num)
    ->order(col, 'asc')
    ->select();
=> SELECT * FROM table WHERE col = val ORDER BY col ASC LIMIT num
```

#### 1.3、获取某个字段或者某个列的值

[](#13获取某个字段或者某个列的值)

```
// 获取某行某个字段的值
Model::invoke()->where(col, val)->value(col)
=> SELECT col FROM table WHERE col = val LIMIT 1

// 获取某个列的值
Model::invoke()->where(col, val)->value(col)
=> SELECT col FROM table WHERE col = val

// 获取多个列的值
Model::invoke()->where(col, val)->value(col1, col2)
Model::invoke()->where(col, val)->value('col1,col2')
=> SELECT col1,col2 FROM table WHERE col = val
```

#### 1.4、数据分批处理

[](#14数据分批处理)

```
Model::invoke()->chunk(3, function ($objs) {

});
=> SELECT * FROM table WHERE primary_key > 1 ORDER BY primary_key ASC LIMIT 1
=> SELECT * FROM table WHERE primary_key > 2 ORDER BY primary_key ASC LIMIT 1
=> SELECT * FROM table WHERE primary_key > 3 ORDER BY primary_key ASC LIMIT 1
```

### 新增

[](#新增)

#### 1.5、添加1条数据

[](#15添加1条数据)

```
# save 方法新增数据返回写入的记录数

$model = Model::invoke();
$model->col = val;
$model->save();
=> INSERT INTO table (col) VALUES (val)

// 批量赋值
$model = Model::invoke([
    col1 => val1,
    col2 => val2
]);
$model->save();
// 或者
$model = Model::invoke();
$model->data([
    col1 => val1,
    col2 => val2
]);
$model->save();
=> INSERT INTO table (col1, col2) VALUES (val1, val2)

// 只保存指定字段
$model = Model::invoke([
    col1 => val1,
    col2 => val2
]);
$model->allowField([col1])->save();
=> INSERT INTO table (col1) VALUES (val1)
```

#### 1.6、获取自增id

[](#16获取自增id)

```
$model = Model::invoke();
$model->col = val;
$model->save();
// 获取自增id
echo $model->primary_key;
=> INSERT INTO table (col) VALUES (val)
```

#### 1.7、添加多条数据

[](#17添加多条数据)

```
// 默认为新增操作
// - 当保存的数据中包含主键时则自动识别为更新操作
$model = Model::invoke();
$model->saveAll([
    [col1 => val1, col2 => val2],
    [col1 => val3, col2 => val4],
]);
=> INSERT INTO table(col1, col2) VALUES (val1, val2);
=> INSERT INTO table(col1, col2) VALUES (val3, val4);

// 更新（saveAll的数据中包含 primary_key）
$model = Model::invoke();
$model->saveAll([
    [primary_key => val11, col1 => val1, col2 => val2],
    [primary_key => val22, col1 => val3, col2 => val4],
]);
=> UPDATE table SET col1 = val1, col2 = val2 WHERE primary_key = val11;
=> UPDATE table SET col1 = val3, col2 = val4 WHERE primary_key = val22;

// 强制批量新增
$model = Model::invoke();
$model->saveAll([
    [primary_key => val11, col1 => val1, col2 => val2],
    [primary_key => val22, col2 => val3, col2 => val4],
], false);
=> INSERT INTO table(primary_key, col1, col2) VALUES (val11, val1, val2);
=> INSERT INTO table(primary_key, col1, col2) VALUES (val22, val3, val4);

// 使用静态方法 create 方法新增
// 返回当前模型的对象实例
$model = Model::create([
    col1 => val1,
    col2 => val2
]);
=> INSERT INTO table (col1, col2) VALUES (val1, val2)
```

### 更新

[](#更新)

#### 1.8、查找并更新

[](#18查找并更新)

```
$model = Model::invoke()->get(1);
$model->col = val;
$model->save();
=> SELECT * FROM table WHERE primary_key = 1 LIMIT 1
=> UPDATE table SET col = val  WHERE primary_key = 1
```

#### 1.9、直接更新数据

[](#19直接更新数据)

```
$model = Model::invoke();
$model->save([
    col => val
], [where_col => where_val]);
=> UPDATE table SET col = val  WHERE where_col = where_val

// 只更新指定字段
$model = Model::invoke();
$model->allowField(['col'])->save([
    col => val,
    col1 => val1
], [where_col => where_val]);
=> UPDATE table SET col = val  WHERE where_col = where_val
```

#### 1.10、批量更新数据

[](#110批量更新数据)

```
上述 saveAll 保存的数据包括主键即可。
```

> 批量更新仅能根据主键值进行更新，其它情况请使用`foreach`遍历更新。

```
// 显式指定
// 强制根据主键条件批量更新数据
$model = Model::invoke();
$model->isUpdate()->saveAll([
    [primary_key => val11, col1 => val1, col2 => val2],
    [primary_key => val22, col1 => val3, col2 => val4],
]);
=> UPDATE table SET col1 = val1, col2 = val2 WHERE primary_key = val11;
=> UPDATE table SET col1 = val3, col2 = val4 WHERE primary_key = val22;
```

#### 1.11、使用 update 方法进行更新

[](#111使用-update-方法进行更新)

```
// 使用 where 条件更新
$model = Model::invoke();
$model->where(col, val)->update([col1 => val1]);
=> UPDATE table SET col1 = val1 WHERE col = val;

// 使用主键条件更新
// 更新的数据包含主键列时 无需使用 where 方法
$model = Model::invoke();
$model->update([primary_key => val11, col => val]);
=> UPDATE table SET col = val WHERE primary_key = val11;
```

#### 1.12、闭包方法更新

[](#112闭包方法更新)

```
$model = Model::invoke();
$model->save([col2 => val2], function ($query) {
    $query->where(col, val)->where(col1, val1, '>');
});
=> UPDATE table SET col2 = val2 WHERE (col = val AND col1 > val1)
```

#### 1.13、自动识别

[](#113自动识别)

```
// 显式指定更新数据
$model = Model::invoke();
$model->isUpdate(true)
    ->save([primary_key => 1, col => val]);
=> UPDATE table SET col = val WHERE primary_key = 1
```

### 删除

[](#删除)

#### 1.14、删除当前模型

[](#114删除当前模型)

```
$model = Model::invoke()->get(1);
$model->delete();
=> DELETE FROM table WHERE primary_key = 1
```

#### 1.15、根据主键删除

[](#115根据主键删除)

```
$model = Model::invoke();
$model->destroy(1);
=> DELETE FROM table WHERE primary_key = 1

$model = Model::invoke();
$model->destroy('1,2,3');
$model->destroy([1,2,3]);
=> DELETE FROM table WHERE primary_key = 1;
=> DELETE FROM table WHERE primary_key = 2;
=> DELETE FROM table WHERE primary_key = 3;
```

#### 1.16、条件删除

[](#116条件删除)

```
$model = Model::invoke();
$model->destroy([col => val]);
=> DELETE FROM table WHERE col = val;

// 闭包删除
$model = Model::invoke();
$model->destroy(function ($query) {
    $query->where(col, val, '>');
});
=> SELECT * FROM table WHERE col > val
=> DELETE FROM table WHERE primary_key = key1
=> DELETE FROM table WHERE primary_key = key2
=> DELETE FROM table WHERE primary_key = key3
...
=> DELETE FROM table WHERE primary_key = keyX
// keyX 取决于最前面查询返回的所有记录的 主键id

// 根据 where 查询条件删除
$model = Model::invoke();
$model->where(col, val, '>')->delete();
=> DELETE FROM table WHERE col > val;
```

### 聚合

[](#聚合)

#### 1.17、count

[](#117count)

```
$model = Model::invoke();
$model->count();
=> SELECT COUNT(*) AS es_count FROM table LIMIT 1

$model = Model::invoke();
$model->count(col);
=> SELECT COUNT(col) AS es_count FROM table LIMIT 1

$model = Model::invoke();
$model->where(col, val, '>')->count();
=> SELECT COUNT(*) AS es_count FROM table WHERE col > val LIMIT 1
```

#### 1.18、max

[](#118max)

```
$model = Model::invoke();
$model->max('age');
=> SELECT MAX(col) AS es_max FROM table LIMIT 1
```

#### 1.19、min

[](#119min)

```
$model = Model::invoke();
$model->min(col);
=> SELECT MIN(col) AS es_min FROM table LIMIT 1
```

#### 1.20、avg

[](#120avg)

```
$model = Model::invoke();
$mode->where(col, val)->avg(col);
=> SELECT AVG(col) AS es_avg FROM table WHERE col = val LIMIT 1
```

#### 1.21、sum

[](#121sum)

```
$model = Model::invoke();
$model->sum(col);
=> SELECT SUM(col) AS es_sum FROM table WHERE col = val  LIMIT 1

$model = Model::invoke();
$mode->where(col, val)->SUM(col);
=> SELECT SUM(col) AS es_sum FROM table WHERE col = val  LIMIT 1
```

官网文档
----

[](#官网文档)

单元测试
----

[](#单元测试)

```
 ./vendor/bin/co-phpunit tests
```

推荐使用mysql著名的employees样例库进行测试和学习mysql: [https://github.com/datacharmer/test\_db](https://github.com/datacharmer/test_db)

主要项目负责人
-------

[](#主要项目负责人)

- 宣言(Siam)

参与贡献方式
------

[](#参与贡献方式)

- 有实际生产使用的，提出升级迭代建议
- 使用过程中遇到问题，并且查看文档，基本排除个人原因导致的问题，怀疑bug，及时反馈
- 参与orm组件的代码维护、功能升级
- 参与orm组件的文档维护（也就是加入easyswoole文档维护团队）

开源协议
----

[](#开源协议)

Apache-2.0

功能介绍
----

[](#功能介绍)

- 基于easyswoole/mysqli组件，承当构造层职责。
- 基于easyswoole/pool组件，承当基础连接池。
- 支持执行自定义sql语句、构造器查询。
- 支持事务，DbManager连接管理器也可承当事务管理器的职责。
- 支持关联查询。
- 支持多数据库配置，读写分离。
- 便捷的连贯操作、聚合操作。

设计层级
----

[](#设计层级)

[![设计层级](https://camo.githubusercontent.com/949de591b05eb4d6b72398ebd88018983ae62ab8bb4fdeed37927c80604d2e81/687474703a2f2f7777772e6561737973776f6f6c652e636f6d2f496d616765732f4f726d2f2545382541452542452545382541452541312545352542312538322545372542412541372e737667)](https://camo.githubusercontent.com/949de591b05eb4d6b72398ebd88018983ae62ab8bb4fdeed37927c80604d2e81/687474703a2f2f7777772e6561737973776f6f6c652e636f6d2f496d616765732f4f726d2f2545382541452542452545382541452541312545352542312538322545372542412541372e737667)

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance100

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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

Unknown

Total

1

Last Release

1d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d45eac5fce75ac651be5041550b27e6555568bfc2b4165cb5f2951cab36be6fc?d=identicon)[shengdoushi](/maintainers/shengdoushi)

---

Top Contributors

[![kiss291323003](https://avatars.githubusercontent.com/u/24490609?v=4)](https://github.com/kiss291323003 "kiss291323003 (256 commits)")[![xuanyanwow](https://avatars.githubusercontent.com/u/28777109?v=4)](https://github.com/xuanyanwow "xuanyanwow (151 commits)")[![Player626](https://avatars.githubusercontent.com/u/44792981?v=4)](https://github.com/Player626 "Player626 (76 commits)")[![tioncico](https://avatars.githubusercontent.com/u/31308307?v=4)](https://github.com/tioncico "tioncico (16 commits)")[![XueSiLf](https://avatars.githubusercontent.com/u/31389659?v=4)](https://github.com/XueSiLf "XueSiLf (11 commits)")[![hlhill](https://avatars.githubusercontent.com/u/29574366?v=4)](https://github.com/hlhill "hlhill (7 commits)")[![evalor](https://avatars.githubusercontent.com/u/26944445?v=4)](https://github.com/evalor "evalor (6 commits)")[![RunsTp](https://avatars.githubusercontent.com/u/28254581?v=4)](https://github.com/RunsTp "RunsTp (2 commits)")[![wqzhaoo](https://avatars.githubusercontent.com/u/58063132?v=4)](https://github.com/wqzhaoo "wqzhaoo (2 commits)")[![LuffyQAQ](https://avatars.githubusercontent.com/u/35624300?v=4)](https://github.com/LuffyQAQ "LuffyQAQ (1 commits)")[![zwytan](https://avatars.githubusercontent.com/u/21805904?v=4)](https://github.com/zwytan "zwytan (1 commits)")[![lpong](https://avatars.githubusercontent.com/u/18649947?v=4)](https://github.com/lpong "lpong (1 commits)")[![huizhang001](https://avatars.githubusercontent.com/u/26496638?v=4)](https://github.com/huizhang001 "huizhang001 (1 commits)")[![shengdoushi](https://avatars.githubusercontent.com/u/1235916?v=4)](https://github.com/shengdoushi "shengdoushi (1 commits)")

---

Tags

asyncframeworkswooleeasyswoole

### Embed Badge

![Health badge](/badges/easy-swoole-orm/health.svg)

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

###  Alternatives

[easyswoole/easyswoole

An efficient swoole framework

4.8k185.9k46](/packages/easyswoole-easyswoole)[easyswoole/fast-cache

An efficient swoole framework

1030.3k](/packages/easyswoole-fast-cache)[easyswoole/actor

easyswoole component

1410.3k](/packages/easyswoole-actor)[easyswoole/words-match

An efficient swoole framework

171.5k2](/packages/easyswoole-words-match)

PHPackages © 2026

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