PHPackages                             hubeiwei/yii2-tools - 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. hubeiwei/yii2-tools

ActiveYii2-extension

hubeiwei/yii2-tools
===================

2.0.x-dev(7y ago)452MITPHP

Since Dec 29Pushed 7y ago1 watchersCompare

[ Source](https://github.com/hubeiwei/yii2-tools)[ Packagist](https://packagist.org/packages/hubeiwei/yii2-tools)[ Docs](https://github.com/hubeiwei/yii2-tools)[ RSS](/packages/hubeiwei-yii2-tools/feed)WikiDiscussions 2.0 Synced 2mo ago

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

yii2-tools
==========

[](#yii2-tools)

使用 yii2 积累下来的一些东西，并不是为了有多好用，仅仅是为了减少每次调用时要写的代码罢了。

目录
--

[](#目录)

- [安装](#%E5%AE%89%E8%A3%85)
- [查询](#%E6%9F%A5%E8%AF%A2)

    - [准备工作](#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C)
    - [开始使用](#%E5%BC%80%E5%A7%8B%E4%BD%BF%E7%94%A8)
    - [日期范围查询的配置](#%E6%97%A5%E6%9C%9F%E8%8C%83%E5%9B%B4%E6%9F%A5%E8%AF%A2%E7%9A%84%E9%85%8D%E7%BD%AE)
- [验证器](#%E9%AA%8C%E8%AF%81%E5%99%A8)
- [Widget](#widget)
- [消息提示](#%E6%B6%88%E6%81%AF%E6%8F%90%E7%A4%BA)
- [在 View 如何更好的把 js 和 css 注入到布局](#%E5%9C%A8-view-%E5%A6%82%E4%BD%95%E6%9B%B4%E5%A5%BD%E7%9A%84%E6%8A%8A-js-%E5%92%8C-css-%E6%B3%A8%E5%85%A5%E5%88%B0%E5%B8%83%E5%B1%80)
- [打赏](#%E6%89%93%E8%B5%8F)

安装
--

[](#安装)

执行：

```
composer require hubeiwei/yii2-tools 2.0.x-dev

```

或者添加：

```
"hubeiwei/yii2-tools": "2.0.x-dev"

```

因为是我自己用的东西，灵活性不一定高，如果你觉得这些代码不能100%满足你，你需要进行一些改动的话，你可以直接把代码下载到 vendor 目录外，添加：

```
"autoload": {
    "psr-4": {
        "hubeiwei\\yii2tools\\": "path/to/yii2-tools"
    }
}

```

然后把该项目的 composer.json 文件里 require 的包都加到你自己的 composer.json 里，执行 `composer update`。

查询
--

[](#查询)

### 准备工作

[](#准备工作)

以下3种方法自选一种：

1:如果你 model 继承的类还是 `yii\db\ActiveRecord`，你可以改成 `hubeiwei\yii2tools\db\ActiveRecord`。

2:如果你已经有了自己的 `ActiveRecord` 类，但并没有 `ActiveQuery` 类，你可以把 `find()` 方法改成以下代码：

```
public static function find()
{
    return Yii::createObject('hubeiwei\yii2tools\db\ActiveQuery', [
        get_called_class(),
        [
            'timeRangeSeparator' => '-',
        ],
    ]);
}
```

3:如果你已经有了自己的 `ActiveQuery` 和 `Query` 类，你可以直接引入我的 trait：`\hubeiwei\yii2tools\db\ActiveQuery\QueryTrait`。

### 开始使用

[](#开始使用)

实例化 `ActiveQuery` 或 `Query`：

```
$query = \common\models\User::find();
// or
$query = new \hubeiwei\yii2tools\db\Query([
    'timeRangeSeparator' => '-',
]);
```

数字范围查询：

```
// WHERE money = 1
$query->compare('money', 1);

// WHERE money > 1 AND money < 3 AND money = 2
$query->compare('money', '>1,,
