L
L
Lumore2016-12-28 00:43:21
Yii
Lumore, 2016-12-28 00:43:21

Slug not generated when post is created?

I use Sluggable Behavior, I want a unique slug to be automatically generated when creating an entry:

public function behaviors()
    {
        return [
            [
                'class' => SluggableBehavior::className(),
                'attribute' => 'name',
                'ensureUnique' => true
            ],
            'timestampBehavior' => [
                'class' => TimestampBehavior::className(),
            ],
        ];
    }

And in the controller:
if ($model->load(Yii::$app->request->post())) {
            $model->user_id = Yii::$app->user->id;
            $model->status = Place::STATUS_ACTIVE;
            // $model->slug = \yii\helpers\BaseInflector::slug($model->name); // Саначала решил проблему этим, но это генерирует не уникальный слаг
            if($model->save(false)) {
                return $this->redirect(['/'.$model->slug]);
            }
        }

Here is the error, respectively:
SQLSTATE[HY000]: General error: 1364 Field 'slug' doesn't have a default value

If you click "Update" and save unchanged, it will only be generated then, but I need it to be generated from the very creation.
What is the problem? In past projects everything was fine, now something went wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2016-12-28
@Lumore

you specify not to validate the data $model->save(false) , and there it seems to bind to the event to generate before the data validation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question