php - Yii 2.0 - Input values saved as null in database -
in practice app, have table called 'music' 3 columns - id, title , artist. whenever try insert input values form database, new record added id has value, title , artist both null. below model:
<?php namespace app\models; /** * model class table "music". * * @property integer $id * @property string $title * @property string $artist */ class musicentry extends \yii\db\activerecord { public $title; public $artist; public $id; public static function tablename() { return 'music'; } public function rules() { return [ [['title', 'artist'], 'required'], [['id'], 'safe'], ]; } } ?>
while controller action looks so:
public function actionmusicentry() { $model = new musicentry (); if (isset ( $_post ['musicentry'] )) { $model->load($_post); if ($model->save()) { yii::$app->session->setflash ( 'success', 'model has been saved' ); $this->redirect ( [ 'music-entry', 'id' => $model->id ] ); } } return $this->render ( 'music-entry', [ 'model' => $model ] ); }
i've tried getting value of artist , title after loading model using $_post , has values inputted in form. given this, why input values saved null in database?
after further tweaking, found cause of problem in model. had remove declaration $artist , $title. i'm still not sure though why adding variables caused such problem. still looking it.
Comments
Post a Comment