B
B
bismoney2019-01-06 15:52:27
PostgreSQL
bismoney, 2019-01-06 15:52:27

Why can't save the record?

Kind guys.
I am getting an error while saving the record.
What's wrong? How to pass him the ID of the new record?

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, , , , , , , null, null, null, null, null).
The SQL being executed was: INSERT INTO "blog" ("id", "name", "title", "title_mobile", "url", "body_start", "body_end") VALUES (NULL, '', '', '', '', '', '') RETURNING "id"

public function actionEdit()
    {
        $model = new Blog();
        $post = Yii::$app->request->post();
        $get = Yii::$app->request->get();

       $id = Yii::$app->request->get('id');
        if ($id) {
            $model = Blog::find()->where(['id' => $id])->one();
            if (!$model) {
                return $this->render('index', [
                    'name' => 'Ошибка',
                    'message' => 'Блог не найден',
                ]);
            }
        }
     
        if ($model->load(Yii::$app->request->post())) {
                $model->save();
                $this->redirect(Url::to(['blog/index']));
            } 
   
        return $this->render('edit', [
            'model' => $model,
        ]);
    }

public function rules()
    {
        return [
            [['check_main', 'check_comment', 'check_status', 'id_project', 'type'], 'integer'],
            [['name', 'title', 'title_mobile', 'url', 'body_start', 'body_end'], 'string'],
        ];
    }

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Decadal, 2019-01-06
@bismoney

Remove from the list of fields ID, and from the list of values ​​the very first (null)
If the request itself is generated and you do not control it, then you have the wrong type for id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question