Hello,
I’m new in programming using Yii and today ran into a problem.
When I’m trying to save a model, I’m getting the error:
`Database Exception – yii\db\Exception
## SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value
The SQL being executed was: INSERT INTO `templates` (`id`) VALUES (DEFAULT)`
Data is getting from form. Fields of the form are not blank.
The code of the form and controller was generated by Gii.
The code of model is:
namespace app\models;
use yii\db\ActiveRecord;
use Yii;
class Templates extends ActiveRecord
{
public function rules()
{
return [
[['name', 'code'], 'required'],
[['code'], 'string'],
[['name'], 'string', 'max' => 255],
[['name', 'code'], 'safe'],
];
}
public static function tableName()
{
return 'templates';
}
}
The code of controller:
namespace app\controllers;
use Yii;
use app\models\Templates;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* TemplatesController implements the CRUD actions for Templates model.
*/
class TemplatesController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Templates models.
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Templates::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Templates model.
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Templates model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model = new Templates();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Templates model.
* If update is successful, the browser will be redirected to the 'view' page.
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing Templates model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Templates model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*/
protected function findModel($id)
{
if (($model = Templates::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
And… Database structure:
I just can’t understand causes of the error and why the data is not inserting into SQL request.
Can anybody help me?
Thank you.