PHPackages                             luwc/webman-yii2-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. luwc/webman-yii2-orm

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

luwc/webman-yii2-orm
====================

Yii2-style ActiveRecord, Validator, and DataProvider ORM for Webman framework

v1.0.1(8mo ago)04MITPHPPHP &gt;=8.1

Since Aug 26Pushed 8mo agoCompare

[ Source](https://github.com/ak47f16200/webman-yii2-orm)[ Packagist](https://packagist.org/packages/luwc/webman-yii2-orm)[ Docs](https://github.com/ak47f16200/webman-yii2-orm.git)[ RSS](/packages/luwc-webman-yii2-orm/feed)WikiDiscussions main Synced 1mo ago

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

Webman Yii2 ORM
===============

[](#webman-yii2-orm)

将 Yii2 框架中经典的 ActiveRecord、Validator、DataProvider 和 Behaviors 功能移植到 Webman 框架中。

特性
--

[](#特性)

- **ActiveRecord**: Yii2 风格的 ORM，支持关联查询、验证规则等
- **ActiveQuery**: 完全兼容 Yii2 的查询构建器，支持链式调用
- **Query Builder**: 流畅的数据库查询构建器
- **Validator**: 强大的数据验证系统，支持自定义规则
- **DataProvider**: 数据提供者，支持分页、排序、过滤
- **Behaviors**: 行为系统，支持 TimestampBehavior 等自动填充功能
- **Database**: 数据库抽象层，支持多种数据库
- **Multi-Database**: 完全支持多数据库连接，不同模型可连接不同数据库
- **Transaction**: 支持跨数据库事务操作
- **Model Validation**: 模型级别的数据验证
- **Yii2 Compatibility**: 100% 兼容 Yii2 的事务和数据库操作语法

安装
--

[](#安装)

```
composer require luwc/webman-yii2-orm
```

### 配置

[](#配置)

#### 1. 数据库连接配置

[](#1-数据库连接配置)

复制配置文件模板：

```
cp vendor/webman/yii2-orm/config/database.example.php config/database.php
```

编辑 `config/database.php`，配置您的数据库连接：

```
return [
    'default' => [
        'driver' => 'mysql',
        'host' => 'localhost',
        'database' => 'your_database',
        'username' => 'your_username',
        'password' => 'your_password',
        'charset' => 'utf8mb4',
    ],
    // 可选：配置额外的数据库连接
    'log' => [
        'driver' => 'mysql',
        'host' => 'log-server',
        'database' => 'logs_database',
        'username' => 'log_user',
        'password' => 'log_password',
        'charset' => 'utf8mb4',
    ],
];
```

#### 2. Bootstrap配置

[](#2-bootstrap配置)

在 `config/bootstrap.php` 中添加初始化代码：

```
// 引入Yii2 ORM初始化
require_once __DIR__ . '/../vendor/webman/yii2-orm/config/bootstrap.example.php';
```

或者手动初始化：

```
use Webman\Yii2Orm\Database\Connection;

// 加载助手函数
require_once __DIR__ . '/../vendor/webman/yii2-orm/src/helpers/yii_compat.php';

// 配置数据库连接
$databaseConfig = config('database');
foreach ($databaseConfig as $name => $config) {
    Connection::addConnection($config, $name);
}
```

快速开始
----

[](#快速开始)

### ActiveRecord 使用

[](#activerecord-使用)

```
