我创建了一个迁移来向表中插入一些数据:
$transaction = $this->getDb()->beginTransaction();
$widget= \Yii::createObject([
'class' => Widget::className(),
'scenario' => 'create',
'title' => 'Testimonial',
'source' => 'testimonial',
'content' => ''
]);
if (!$widget->insert(false)) {
$transaction->rollBack();
return false;
}
$transaction->commit();
在模型中,我配置了行为:
'sluggable' => [
'class' => SluggableBehavior::className(),
'attribute' => 'title',
'ensureUnique' => true
]
运行迁移 slug 后字段为空。我还有其他行为:Timestamp、Blameable 和 Ip,它们工作正常。
有任何想法吗?