sizeg/yii2-newton-cool-ranking-behavior

Yii2 Newton cool ranking behavior

v1.0.0 2016-06-21 10:35 UTC

This package is not auto-updated.

Last update: 2024-09-20 19:19:49 UTC


README

此行为提供了牛顿冷却定律算法,由Evan Miller解释。

您可以使用它来评分评论或博客文章。列出在线论坛中的活跃讨论线程。

阅读此文章使用牛顿冷却定律进行热排名以获取更多详细信息。

安装

该软件包可在Packagist上找到,您可以使用Composer进行安装。

composer require sizeg/yii2-newton-cool-ranking-behavior

依赖关系

  • Yii2 (已测试2.8版本,但应适用于较低版本)

基本用法

创建迁移,

public function up()
{
    // [[NewtonCoolRankingBehavior::$rankAttribute]]
    $this->addColumn('{{%tableName}}', 'rank', $this->float());

    // [[NewtonCoolRankingBehavior::$rankTimeAttribute]]
    // By default time update with result of php time() function
    // For example we will use DateTime instead of UnixTimestamp
    $this->addColumn('{{%tableName}}', 'rankTime', $this->datetime());

    // [[NewtonCoolRankingBehavior::$rankBoostAttribute]]
    // This field is optional
    $this->addField('{{%tableName}}', 'rankBoost', $this->float());
}

将行为添加到您的ActiveRecord模型中,

class Item extends \yii\base\ActiveRecord
{
    public function behaviors()
    {
        return \yii\helpers\ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => 'sizeg\newtoncoolranking\NewtonCoolRankingBehavior',
                // optional params
                'initial' => 1000, 
                'coolingRate' => 150,
                'timeValue' => date('Y-m-d H:i:s'), // can be a callback function
            ]
        ]);
    }
}

默认情况下,新模型将具有[[NewtonCoolRankingBehavior::$initial]]值,并使用[[NewtonCoolRankingBehavior::$coolingRate]]进行冷却。

当模型有新的活动时,您需要更新排名,

/** @var ActiveRecord $model */
$model->heat(20);

有时您需要将一个或多个模型展示在顶部几天,那么您需要提升它。

提升值将从模型的[[NewtonCoolRankingBehavior::$rankBoostAttribute]]字段获取。如果字段不存在,则从可选的[[NewtonCoolRankingBehavior::$boost]]属性获取值。

/** @var ActiveRecord $model */
$model->boost();