J
J
JohnDaniels2018-12-12 12:51:45
Yii
JohnDaniels, 2018-12-12 12:51:45

How to update a record without changing updated_at in Yii2?

It was required in one specific case, when changing the model, do not change updated_at.

public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
     ]
}

public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $model->scenario = Model::SCENARIO_UPDATE_STATUS;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['/models']);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

How would you do?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question