提问者:小点点

YII2-迁移时不起作用的迟缓行为


我创建了一个迁移,将一些数据插入到表中:

$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
            ]

运行后,迁移段块字段为空。 我有其他的行为:时间戳,可责备的,和Ip他们工作得很好。

有什么想法吗?


共1个答案

匿名用户

我已经解开了谜团。 SlaggableBehavior是在“验证之前”触发的,所以我只需要补充一下:

"$widget->validate();"

最终代码:

/*Widget*/
        $transaction = $this->getDb()->beginTransaction();

        $widget= \Yii::createObject([
            'class'    => Widget::className(),
            'title' => 'Testimonial',
            'source' => 'testimonial',
            'content'    => ''
        ]);

        $widget->validate();
        if (!$widget->insert(false)) {
            $transaction->rollBack();
            return false;
        }

        $transaction->commit();