Hi guys.
I can’t find a simple way to to make Yii2 insert NULLs instead of 0 into INT fields.
Is there any? (besides using beforeSave)
Hi guys.
I can’t find a simple way to to make Yii2 insert NULLs instead of 0 into INT fields.
Is there any? (besides using beforeSave)
Can you show your table schema?
Hmm, interesting.
I’ve created a test table to show
CREATE TABLE tbl_nulltest (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
var1 INT UNSIGNED NULL,
var2 INT NULL,
var3 INT DEFAULT NULL,
PRIMARY KEY (id)
);
and found that 0 is stored only in var1, var2 and var3 are getting NULL values.
Is this because of UNSIGNED?
Looks like a bug to me.
added a unit test to verify behavior. Not sure what you are doing wrong but tests are passing:
https://travis-ci.org/yiisoft/yii2/builds/13223241
Can you show your code for storing the values?
Here’s the model:
<?php
namespace app\models;
class Nulltest extends \yii\db\ActiveRecord
{
public function rules()
{
return array(
array(
'var1, var2, var3',
'number',
'message' => 'numbers only'
),
);
}
}
Here’s the controller:
public function actionTest()
{
$record = new \app\models\Nulltest;
if ($record->load($_POST)) {
if ($record->save()) {
Yii::$app->session->setFlash('success', 'saved');
Yii::$app->response->redirect(Html::url(array('index')));
} else {
Yii::$app->session->setFlash('error', 'Error');
}
}
return $this->render('test', array(
'record' => $record,
));
}
I suppose I should investigate further.
Actually, this should be added to test also:
$record->var1 = '';
$record->var2 = '';
$record->var3 = '';
$record->stringcol = '';
to simulate form submit with empty fields.