PHPackages                             wzzwx/yii1-model-extension - 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. wzzwx/yii1-model-extension

ActiveYii1-extension[Database &amp; ORM](/categories/database)

wzzwx/yii1-model-extension
==========================

yii1 model helper extension

1.1.0(3y ago)05MITPHP

Since Jun 21Pushed 3y ago1 watchersCompare

[ Source](https://github.com/wzzwx/yii1-model-extension)[ Packagist](https://packagist.org/packages/wzzwx/yii1-model-extension)[ RSS](/packages/wzzwx-yii1-model-extension/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (5)Used By (0)

yii1 框架model 查询扩展
=================

[](#yii1-框架model-查询扩展)

### db配置

[](#db配置)

```
// main.php

return [
    ...
    'components' => [
        ...
        'db' => [
            'class' => \wzzwx\yii1model\db\DbConnection::class, // 此处使用
            'connectionString' => ...
            ...
        ],
        ...
    ],
    ...
];

```

### model 定义

[](#model-定义)

```
use wzzwx\yii1model\traits\ModelTrait;

class UserModel
{
    use ModelTrait;

    public static $table = 'user';

    public static function withConfigs()
    {
        return [
            'order' => [    // 用户的订单
                'query' => OrderModel::query(),
                'thisField' => 'id',        // user 表中对应的字段
                'targetField' => 'user_id', // order表总对应的字段
                'type' => DbCommand::WITH_TYPE_MANY,    // 表示一对多
            ],
        ];
    }
}

class OrderModel
{
    use ModelTrait;

    public static $table = 'order';

    public static function withConfigs()
    {
        return [
            'items' => [    // 子订单
                'query' => OrderItemsModel::query(),
                'thisField' => 'id',    // 值为"id"的 此行可省略
                'targetField' => 'order_id',
                'type' => DbCommand::WITH_TYPE_MANY,    // 订单对子订单为 一对多
            ],
            'user' => [
                'query' => UserModel::query()->select('id,name'),
                'thisField' => 'user_id',
                // 'targetField' => 'id',  // id字段可不写
            ],
        ];
    }
}

```

### 查询

[](#查询)

```
$ret = UserModel::find(1);  // 根据id 查询

$ret = UserModel::find([
    'name' => '张三',
    'status' => 1,
]);  // 根据自定义字段查询

$ret = UserModel::query()
    ->andWhere(['name' => 'zhangsan'])
    ->andWhere(['in', 'id', [1,2,3]])
    ->find();

```

### 增加or修改

[](#增加or修改)

```
    // 新增
    $user_id = UserModel::save([
        'name' => '张三',
        'age' => 22,
    ]);

    // 根据id修改
    $user = UserModel::find(1);
    UserModel::save([
        'id' => $user['id'],
        'namg' => '张四',
    ]);
    // 其他条件
    UserModel::update(['name' => '张四'], ['name' => '张三']);

```

### with 用法

[](#with-用法)

#### 可配合model中的withConfigs方法, 也可将配置参数直接传入with方法

[](#可配合model中的withconfigs方法-也可将配置参数直接传入with方法)

```
    // 查询用户的订单
    $ret = UserModel::query()
        ->with('order')     // 在UserModel中配置的withConfigs
        ->queryAll();

    // 查询某个订单
    $ret = OrderModel::query()
        ->with('items,user')
        ->andWhere(['order_no' => 'JD123d1awaasdwa221'])
        ->queryRow();
    // 或者
    $ret = OrderModel::query()
       ->with([
            'user',
            // 'user' => fn($query) => $query->select('id,name,age'),  // php7.4 简写
            'items' => function($query){
                $query->select('xx,xx')
                    ->andWhere(['status' => xxx])     // 增加条件筛选
                    ->with('goods');    // 查出产品相关信息, 需要在OrderItems中配置好withConfigs
            }
        ])
       ->andWhere(['order_no' => 'JD123d1awaasdwa221'])
       ->queryRow();
    // 也可将 model 中 withConfigs()方法的返回值直接传入with()方法

```

### 分页查询

[](#分页查询)

```
    $query = UserModel::query()
        ->with('order')
        ->andWhere(['user_id' => 123]);

    $list = $query
        ->pageing(10, 1)
        // ->pageing()          // 默认取 page 和 pageSize 参数的值
        ->queryAll();           // 分页数据

    $count = $query->count();   // 总数量

```

如有扩展性问题, 建议建子类重写, 后续有时间会完善

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~11 days

Total

4

Last Release

1387d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/23d6aec20fa876e745ff5f2ce7e7a00cdc32d61a1a96a15666587ff22b217486?d=identicon)[xiayutian](/maintainers/xiayutian)

---

Top Contributors

[![wzzwx](https://avatars.githubusercontent.com/u/54147959?v=4)](https://github.com/wzzwx "wzzwx (1 commits)")

---

Tags

modeldbYii1

### Embed Badge

![Health badge](/badges/wzzwx-yii1-model-extension/health.svg)

```
[![Health](https://phpackages.com/badges/wzzwx-yii1-model-extension/health.svg)](https://phpackages.com/packages/wzzwx-yii1-model-extension)
```

###  Alternatives

[symlex/doctrine-active-record

Object-oriented CRUD for Doctrine DBAL

308.7k2](/packages/symlex-doctrine-active-record)

PHPackages © 2026

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