How to save ActiveForm to database? Why I get “Integrity constraint violation”?











up vote
-2
down vote

favorite












I have this test app written in Yii 2.0. One model, one form.
When I click save button I got error:



Integrity constraint violation – yiidbIntegrityException
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(). Why my form is unable to save my data to database?



sql



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


file db.php



<?php
return [
'class' => 'yiidbConnection',
'dsn' => 'sqlite:@app/sqlite3.db',
];


file Test.php



<?php
namespace appmodels;
use yiidbActiveRecord;

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 yiihelpersHtml;
use yiiwidgetsActiveForm;

$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
]);
}









share|improve this question




























    up vote
    -2
    down vote

    favorite












    I have this test app written in Yii 2.0. One model, one form.
    When I click save button I got error:



    Integrity constraint violation – yiidbIntegrityException
    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(). Why my form is unable to save my data to database?



    sql



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


    file db.php



    <?php
    return [
    'class' => 'yiidbConnection',
    'dsn' => 'sqlite:@app/sqlite3.db',
    ];


    file Test.php



    <?php
    namespace appmodels;
    use yiidbActiveRecord;

    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 yiihelpersHtml;
    use yiiwidgetsActiveForm;

    $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
    ]);
    }









    share|improve this question


























      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite











      I have this test app written in Yii 2.0. One model, one form.
      When I click save button I got error:



      Integrity constraint violation – yiidbIntegrityException
      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(). Why my form is unable to save my data to database?



      sql



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


      file db.php



      <?php
      return [
      'class' => 'yiidbConnection',
      'dsn' => 'sqlite:@app/sqlite3.db',
      ];


      file Test.php



      <?php
      namespace appmodels;
      use yiidbActiveRecord;

      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 yiihelpersHtml;
      use yiiwidgetsActiveForm;

      $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
      ]);
      }









      share|improve this question















      I have this test app written in Yii 2.0. One model, one form.
      When I click save button I got error:



      Integrity constraint violation – yiidbIntegrityException
      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(). Why my form is unable to save my data to database?



      sql



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


      file db.php



      <?php
      return [
      'class' => 'yiidbConnection',
      'dsn' => 'sqlite:@app/sqlite3.db',
      ];


      file Test.php



      <?php
      namespace appmodels;
      use yiidbActiveRecord;

      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 yiihelpersHtml;
      use yiiwidgetsActiveForm;

      $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
      ]);
      }






      php yii yii2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 13:17









      Cœur

      17.2k9102142




      17.2k9102142










      asked Aug 22 '17 at 16:24









      BPS

      441530




      441530
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          In your model you defined attributes:



          class Test extends ActiveRecord {
          public $col1; // here
          public $col2; // and here


          If you want to work this properly, remove this, ActiveRecord is automatically mapping columns from your table.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45822747%2fhow-to-save-activeform-to-database-why-i-get-integrity-constraint-violation%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            In your model you defined attributes:



            class Test extends ActiveRecord {
            public $col1; // here
            public $col2; // and here


            If you want to work this properly, remove this, ActiveRecord is automatically mapping columns from your table.






            share|improve this answer

























              up vote
              1
              down vote



              accepted










              In your model you defined attributes:



              class Test extends ActiveRecord {
              public $col1; // here
              public $col2; // and here


              If you want to work this properly, remove this, ActiveRecord is automatically mapping columns from your table.






              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                In your model you defined attributes:



                class Test extends ActiveRecord {
                public $col1; // here
                public $col2; // and here


                If you want to work this properly, remove this, ActiveRecord is automatically mapping columns from your table.






                share|improve this answer












                In your model you defined attributes:



                class Test extends ActiveRecord {
                public $col1; // here
                public $col2; // and here


                If you want to work this properly, remove this, ActiveRecord is automatically mapping columns from your table.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 23 '17 at 5:49









                Yupik

                3,8951620




                3,8951620






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45822747%2fhow-to-save-activeform-to-database-why-i-get-integrity-constraint-violation%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    The Sandy Post

                    Danny Elfman

                    Pages that link to "Head v. Amoskeag Manufacturing Co."