nyx-solutions/yii2-nyx-behaviors

Yii2 nyx Behaviors

5.0.0 2022-06-03 03:17 UTC

This package is auto-updated.

Last update: 2024-08-30 01:26:31 UTC


README

Yii Framework 2.0 的实用行为集合(目前只有一个行为,即 datetime 行为)。

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads Daily Downloads composer.lock

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require --prefer-dist nyx-solutions/yii2-nyx-behaviors "*"

或添加

"nyx-solutions/yii2-nyx-behaviors": "*"

到您的 composer.json 文件的 require 部分。

用法

在您的基 ActiveRecord 模型中,可以添加以下 behaviors 方法

namespace common\models;

use \yii\helpers\ArrayHelper;

/**
 * Class ActiveRecordModel
 *
 * @package common\models
 */
class ActiveRecordModel extends \yii\db\ActiveRecord
{
    #region Behaviors
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        $behaviors = [];

        if ($this->hasAttribute('createdAt') && $this->hasAttribute('updatedAt')) {
            $behaviors['datetime'] = [
                'class'      => \nyx\behaviors\DateTimeBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['createdAt', 'updatedAt'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updatedAt'
                ]
            ];
        }
        
        return ArrayHelper::merge(parent::behaviors(), $behaviors);
    }
    #endregion
}

许可证

yii2-nyx-behaviors 在 BSD 3-Clause 许可证下发布。有关详细信息,请参阅附带文件 LICENSE.md

Yii2