nox-it/yii2-nox-behaviors

该软件包已被放弃,不再维护。未建议替代软件包。

Yii2 nyx Behaviors

2.0.0 2020-05-02 21:18 UTC

This package is auto-updated.

Last update: 2022-06-01 20:11:44 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