YII Insert Record

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Create (Insert) Record

To insert the records in the database, following steps are there. We have named our Yii2 folder as create.

Step 1 Create Model fileCreate a model file child.php in

Create a model file child.php in frontend/models folder.

  1. <?php   
  2. namespace appmodels;   
  3.    
  4. use Yii;   
  5.    
  6. class Child extends yiidbActiveRecord   
  7. {   
  8.     /**  
  9.      * @inheritdoc  
  10.      */   
  11.     public static function tableName()   
  12.     {   
  13.         return 'child';   
  14.     }   
  15.        
  16.     /**  
  17.      * @inheritdoc  
  18.      */   
  19.     public function rules()   
  20.     {   
  21.         return [   
  22.             [['name''meaning''gender'], 'required'],   
  23.             [['name''meaning'], 'string''max' => 100],   
  24.             [['gender'], 'string''max' => 15]   
  25.         ];   
  26.     }   
  27. }  

Look at the above code,

  • yiidbActiveRecord is used to create a model file.

  • In function tableName put the table name you are using.

  • Function rules define the inputs of your table.

Step 2 Create Controllers fileCreate a controller file

Create a controller file ChildController.php in frontend/controllers folder.

 
  1. <?php   
  2. namespace frontendcontrollers;   
  3.    
  4. use Yii;   
  5. use appmodelsChild;   
  6. use yiiwebController;   
  7.   
  8. /**  
  9.  * manual CRUD  
  10.  **/   
  11. class ChildController extends Controller   
  12. {    
  13.     /**  
  14.      * Create  
  15.      */   
  16.     public function actionCreate()   
  17.     {   
  18.           
  19.         $model = new Child();   
  20.    
  21.         // new record   
  22.         if($model->load(Yii::$app->request->post()) && $model->save()){   
  23.             return $this->redirect(['index']);   
  24.         }   
  25.                    
  26.         return $this->render('create', ['model' => $model]);   
  27.     }  
  28.     }  

Step 3 Create View file

Create a view folder child in frontend/views folder. Then create a file create.php in frontend/views/child folder.

  1. <?= $this->render('child_view', [   
  2.     'model' => $model,   
  3. ]) ?>  

Now create a file child_view.php in frontend/views/child folder.

  1. <?php   
  2. use yiihelpersHtml;   
  3. use yiiwidgetsActiveForm;   
  4. ?>   
  5.    
  6. <?php $form = ActiveForm::begin(); ?>   
  7.    
  8.     <?= $form->field($model'name'); ?>   
  9.     <?= $form->field($model'meaning'); ?>   
  10.     <?= $form->field($model'gender'); ?>   
  11.       
  12.     <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->   
  13.      isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?>   
  14.    
  15.    <?php ActiveForm::end(); ?>  

Step 4 Run it

Now run your application on your browser with the following URL.

http://localhost/create/frontend/web/index.php?r=child/create

YII Insert record 1

Look at the above snpashot, after filling all the fields click on Create button and your data will be inserted into the database.

You can check it in the database from your phpmyadmin.