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
]);
}
php yii yii2
add a comment |
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
]);
}
php yii yii2
add a comment |
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
]);
}
php yii yii2
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
php yii yii2
edited Nov 11 at 13:17
Cœur
17.2k9102142
17.2k9102142
asked Aug 22 '17 at 16:24
BPS
441530
441530
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Aug 23 '17 at 5:49
Yupik
3,8951620
3,8951620
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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