YII Update Record

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

Update Record

To update the records in the database, following these steps. We have named our Yii2 folder as update.

Step 1 Create Model file

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. }  

Step 2 Add action in controllers

Add the update action actionEdit in controllers file ChildController.php.

  1. <?php   
  2. namespace frontendcontrollers;   
  3.    
  4. use Yii;   
  5. use appmodelsChild;   
  6. use yiiwebController;   
  7.    
  8.   
  9. /**  
  10. * manual CRUD  
  11.  **/   
  12. class ChildController extends Controller   
  13. {    
  14.     /**  
  15.      * Create  
  16.      */   
  17.     public function actionCreate()   
  18.     {   
  19.           
  20.         $model = new Child();   
  21.    
  22.         // new record   
  23.         if($model->load(Yii::$app->request->post()) && $model->save()){   
  24.             return $this->redirect(['index']);   
  25.         }   
  26.                    
  27.         return $this->render('create', ['model' => $model]);   
  28.     }   
  29.   
  30.     /**  
  31.      * Read  
  32.      */   
  33.     public function actionIndex()   
  34.     {   
  35.         $child = Child::find()->all();   
  36.            
  37.         return $this->render('index', ['model' => $child]);   
  38.     }   
  39.   
  40.     /**  
  41.      * Edit  
  42.      * @param integer $id  
  43.      */   
  44.     public function actionEdit($id)   
  45.     {   
  46.         $model = Child::find()->where(['id' => $id])->one();   
  47.    
  48.         // $id not found in database   
  49.         if($model === null)   
  50.             throw new NotFoundHttpException('The requested page does not exist.');   
  51.            
  52.         // update record   
  53.         if($model->load(Yii::$app->request->post()) && $model->save()){   
  54.             return $this->redirect(['index']);   
  55.         }   
  56.            
  57.         return $this->render('edit', ['model' => $model]);   
  58.     }   
  59.     }  

Step 3 Create View file

Create a file edit.php in frontend/view/child folder.

  1. render('child_view', [   

  2.     'model' => $model,   

  3. ]) ?>  

 

Step 4 Run it

Run it on the browser.

http://localhost/dbb/frontend/web/index.php?r=child%2Fedit&id=6

YII Update record 1

Click on Update, information will be updated.

YII Update record 2

Look at the last row in above snapshot, data is updated.