数据模型层
models/ZsDynasty.php
<?php
namespace app\models;
use yii\db\ActiveRecord;
class ZsDynasty extends ActiveRecord
{
}
ZsDynasty 对应数据库表 zs_dynasty
控制器
Controller/TestController.php
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\data\Pagination;//分页
use yii\data\ActiveDataProvider;//活动记录
use app\models\ZsDynasty;//自定义数据模型
class TestController extends Controller
{
public function actionIndex()
{
$query = ZsDynasty::find();
$pagination = new Pagination([
'defaultPageSize' => 15,
'totalCount' => $query->count(),
]);
$dynastys = $query->orderBy('dyn_id')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render('index', [
'dynastys' => $dynastys,
'pagination' => $pagination,
]);
}
}
对应网址 index.php?r=test/index
视图
views\test\index.php
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
$this->title = '朝代';
$this->params['breadcrumbs'][] = $this->title;
?>
<h1>朝代</h1>
<ul>
<?php foreach ($dynastys as $dynasty): ?>
<li>
<?= Html::encode("{$dynasty->dyn_name} ({$dynasty->dyn_id})") ?>:
</li>
<?php endforeach; ?>
</ul>
<?= LinkPager::widget(['pagination' => $pagination]) ?>