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

Step 1 Create Model file

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

  1. <?php   
  2. namespace app\models;   
  3.    
  4. use Yii;   
  5.    
  6. class Child extends \yii\db\ActiveRecord   
  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,

  • \yii\db\ActiveRecord 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 file

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

  1. <?php   
  2. namespace frontend\controllers;   
  3.    
  4. use Yii;   
  5. use app\models\Child;   
  6. use yii\web\Controller;   
  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 yii\helpers\Html;   
  3. use yii\widgets\ActiveForm;   
  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.