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

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

Step 2 Create action in controllers

Create an action actionDelete in ChildController.php file.

  1. <?php   
  2. namespace frontend\controllers;   
  3.    
  4. use Yii;   
  5. use app\models\Child;   
  6. use yii\web\Controller;   
  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.   
  60.      /**  
  61.     * Delete  
  62.      * @param integer $id  
  63.      */   
  64.      public function actionDelete($id)   
  65.      {   
  66.          $model = Child::findOne($id);   
  67.            
  68.         // $id not found in database   
  69.         if($model === null)   
  70.             throw new NotFoundHttpException('The requested page does not exist.');   
  71.                
  72.         // delete record   
  73.         $model->delete();   
  74.            
  75.         return $this->redirect(['index']);   
  76.      }      
  77.    }  

Step 3 Run it

Run it on the browser.

http://localhost/delete/frontend/web/index.php?r=child%2Findex

YII Delete record 1

Look at the above snapshot, click the Delete option in the last row of the table.

Look at the above snapshot, respective row is deleted from the table.

YII Delete record 2