“Integrity constraint violation” while saving model to database.

Hello :)

I’m learning Yii 2.0 and I have this test app written in Yii 2.0.

One model, one form. Why my form is unable to save my data to database?

When I click submit button of my form I get error:


Integrity constraint violation – yii\db\IntegrityException

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: test.col1

The SQL being executed was: INSERT INTO `test` DEFAULT VALUES


Error Info: Array

(

    [0] => 23000

    [1] => 19

    [2] => NOT NULL constraint failed: test.col1

)


↵

Caused by: PDOException

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: test.col1


in /home/bps/workspace/test/YiiTest/vendor/yiisoft/yii2/db/Command.php at line 963



at line with $model->save().

My database has one table


CREATE TABLE test(id INTEGER PRIMARY KEY, col1 VARCHAR(255) NOT NULL, col2 VARCHAR(255) NOT NULL UNIQUE);

I use sqlite3 - file db.php


<?php

return [

    'class' => 'yii\db\Connection',

    'dsn' => 'sqlite:@app/sqlite3.db',

];



file Test.php


<?php

namespace app\models;

use yii\db\ActiveRecord;


class Test extends ActiveRecord {

    public $col1;

    public $col2;


    public static function tableName(){

        return 'test';

    }


    public function rules(){

        return [

            [['col1', 'col2'], 'required']

        ];

    }

}


file test.php

<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;


$this->title = 'test';

?>

<div class="row">

    <div class="col-lg-12">

        <?php $form = ActiveForm::begin(); ?>

        <?= $form->field($model, 'col1')->textInput(); ?>

        <?= $form->field($model, 'col2')->textArea(); ?>

        <?= Html::submitButton(); ?>

        <?php ActiveForm::end(); ?>

    </div>

</div>

<div class="row">

    <div class="col-lg-12">

        <div>record count: <?= $num_records ?></div>

        <ul>

            <?php foreach ($items as $item): ?>

            <li><?= $item->col1 ?></li>

            <?php endforeach; ?>

        </ul>

    </div>

</div>

SiteController.php


public function actionTest(){

        $model = new Test();


        // add items

        if (Yii::$app->request->isPost){

            if ($model->load(Yii::$app->request->post()) && $model->validate()){

                $model->save();

            }

        }


        // schow items

        $items = Test::find();

        return $this->render('test', [

            'num_records' => $items->count(),

            'items' => $items->all(),

            'model' => $model

        ]);

    }



thanks :)

Try it without the variable declarations in your Test model. ActiveRecord reads the table and automatically generates those @properties. I may be that your ‘col1’ and the ‘model->col1’ are two separate variables.

Let use know.

Not sure if ‘num_records’ will ever have a number. I believe that Test::find() only starts the SQL query, it’s not executed until the $items->all() is executed. It MAY work if you swapped the order of your params.


class Test extends ActiveRecord {

    public $col1;

    public $col2;

    ...



Ohh, I mistaken ActiveRecord with Model class, after removing $col1 and $col2 everything is working now.

Thanks :)