V
V
Vladislav2020-02-21 20:44:25
Yii
Vladislav, 2020-02-21 20:44:25

Why doesn't the standard Update CRUD method work?

/**
     * Updates an existing news model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
        	
            return $this->redirect(['view', 'id' => $model->id]);
        }

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

Standard method generated via CRUD Gii.
I change some field in the news, for example, the title.
he doesn't change it. just
passes all the conditions and just executes
return $this->redirect(['view', 'id' => $model->id]);

and with the create method it gives an error in general
Database Exception – yii\db\Exception
SQLSTATE[HY000]: General error: 1364 Field 'caption' doesn't have a default value
The SQL being executed was: INSERT INTO `ab_news` (`id`) VALUES (DEFAULT)
Error Info: Array
(
    [0] => HY000
    [1] => 1364
    [2] => Field 'caption' doesn't have a default value
)
↵
Caused by: PDOException
SQLSTATE[HY000]: General error: 1364 Field 'caption' doesn't have a default value
in W:\domains\localhost1\vendor\yiisoft\yii2\db\Command.php at line 1290

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2020-02-21
@cr1gger

For a model, specify rules with its fields.
Turn on the debug mode, it helps a lot to understand where there is no data.
Pay attention to the request in the error, it contains only the id field.

C
cyberlog, 2020-02-21
@cyberlog

General error: 1364 Field 'caption' doesn't have a default value

You do not pass the caption field, and since it does not have a default value in the database, an error occurs - mysql prohibits doing such INSERT
You must either supplement the form with this value, or add a default value for this (and other unused) fields to the table description .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question