//Test.php (models)
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Test extends ActiveRecord{
// tableName()方法返回要进行操作的表名
public function tableName(){
return 'test';
}
}
//TestController.php
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\Test;
class TestController extends Controller{
public function actionTest(){
//查询表中所有数据
$list = Test::find()->all();
//渲染到模版
return $this->render('test',['list', $list]);
}
}
//test.php (views)
<?php
use yii\helpers\Html;
?>
<div class="country-view">
<ul>
<?php foreach ($test as $t) :?>
<li>
<?= Html::encode("{$t->id} ({$t->name})") ?>:
<?= $t->population ?>
</li>
<?php endforeach; ?>
</ul>
</div>
对照学习手册,写于2018年3月8日 14:48:17