Now we'll fetch the data from our table child. Here our Yiii2 folder is named as read.

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 Add action to read

In the ChildController.php file, we need to add the action actionIndex to fetch the data from the table.

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

Look at the above code, all code is same only action to fetch data is added in the last.

Step 3 Create View file

In the frontend/views/child folder, create a file index.php.

  1. <?php   
  2. use yii\helpers\Html;   
  3. ?>   
  4.    
  5. <style>   
  6. table th,td{   
  7.     padding: 10px;   
  8. }   
  9. </style>   
  10.    
  11. <?= Html::a('Create', ['child/create'], ['class' => 'btn btn-success']); ?>   
  12.    
  13. <table border="1">   
  14.    <tr>   
  15.         <th>Name</th>   
  16.         <th>Meaning</th>   
  17.         <th>Gender</th>   
  18.     </tr>   
  19.     <?php foreach($model as $field){ ?>   
  20.     <tr>   
  21.         <td><?= $field->name; ?></td>   
  22.         <td><?= $field->meaning; ?></td>   
  23.         <td><?= $field->gender; ?></td>   
  24.         <td><?= Html::a("Edit", ['child/edit''id' => $field->id]); ?> | <?= Html::a("Delete", ['child/delete''id' => $field->id]); ?></td>   
  25.     </tr>   
  26.     <?php } ?>  

Step 4 Run it

Run it on the browser,.

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

YII Read record 1