Yii2 SluggableBehavior "attribute" or "value" 属性 must be specified 错误

Yii2 SluggableBehavior "attribute" or "value" property must be specified error

我关注 Programming with YII2 tutorial on tuts+ but when I finish chapter Programming With Yii2: Sluggable Behavior 并尝试访问 /status/ 页面我看到下一条错误消息

我连接到 SluggableBehavior 的模型

namespace app\models;

use Yii;
use yii\behaviors\SluggableBehavior;


class Status extends \yii\db\ActiveRecord
{
    const PERMISSIONS_PRIVATE = 10;
    const PERMISSIONS_PUBLIC = 20;

    public function behaviors()
    {
        return [
            [
                'class' => SluggableBehavior::className(),
                'attribute' => 'message',
                // 'slugAttribute' => 'slug',
            ],
        ];
    }
    . . .

我做错了什么?我在 yii2 目录的 SluggableBehavior class 中阅读了权威指南和使用示例,但没有发现任何特别的东西。

在阅读有关 yii2 和 sluggable 行为的文档和论坛几个小时后,我找到了我需要的东西。

我指定 value 属性 并且一切正常:

'value' => function($event){
    if(!empty($event->sender->slug))
        return $event->sender->slug;
    return Inflector::slug($event->sender->title);
},

你的情况是正确的,但我遇到了同样的问题,但出于其他原因。我试图合并父行为和当前模型行为,但错误地忘记了一级括号 []

  public function behaviors() {
    return array_merge(parent::behaviors(),
      [  // <-- forgot this brackets
        [
          'class'=> \yii\behaviors\SluggableBehavior::className(),
          'attribute'=> ['singleTranslation.title'],
          'immutable' => true,
          'ensureUnique' => true,
          //'slugAttribute' => 'slug'
        ]
      ]  // <-- forgot this brackets
    );
  }