简单的说:
1.先配置好数据库
位置在config/db.php
更改如下:
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=music',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
];
2.新建立个models层文件这里叫做
位置:models/Country.php
代码如下:
<?php
namespace app\models;//所有的model都要继承这个
use yii\db\ActiveRecord;
class Country extends ActiveRecord
{
}
3.新建controllers层文件
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\data\Pagination; //这个使用来进行分页使用的
use app\models\Country; //直接把model层引进来使用
class CountryController extends Controller
{
//下面是查询控制器的方法
public function actionIndex()
{
$query = Country::find();
$pagination = new Pagination([
'defaultPageSize' => 6,
'totalCount' => $query->count(),
]);
$countries = $query->orderBy('name')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
//指向跳转位置页面和携带的参数
return $this->render('index', [
'countries' => $countries,
'pagination' => $pagination,
]);
}
//下面是增加数据库的方法
public function actionAdd(){
$ty = new Country();
$ty->code = 'gd';
$ty->name = 'ceshiname';
$ty->population = '10010';
if(($ty->save())>0){
echo "添加ok";
}else{
echo "fail";
}
}
//下面是删除的方法
public function actionDel(){
$tydel = new Country();
$success = $tydel->deleteAll('name ="ceshiname"');
if($success>0){echo "删除成功"; }else{echo "删除失败"; }
}
//下面是修改方法
public function actionUpdate(){
$tyupdate = new Country();
$kkk = $tyupdate->updateAll(array('code'=>'mH'),'code="CH"');
if($kkk>0){
echo "update success!";
}else{
echo "update fail";
}
}
}
4.建立展现层
位置views/country/index.php
代码如下:
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Countries</h1>
<ul>
<?php foreach ($countries as $country): ?>
<li>
<?= Html::encode("{$country->name} from ({$country->code})") ?>:
<?= $country->population ?>
</li>
<?php endforeach; ?>
</ul>
<?= LinkPager::widget(['pagination' => $pagination]) ?>
说明:name与code都是数据库中存在的字段
1.先配置好数据库
位置在config/db.php
更改如下:
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=music',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
];
2.新建立个models层文件这里叫做
位置:models/Country.php
代码如下:
<?php
namespace app\models;//所有的model都要继承这个
use yii\db\ActiveRecord;
class Country extends ActiveRecord
{
}
3.新建controllers层文件
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\data\Pagination; //这个使用来进行分页使用的
use app\models\Country; //直接把model层引进来使用
class CountryController extends Controller
{
//下面是查询控制器的方法
public function actionIndex()
{
$query = Country::find();
$pagination = new Pagination([
'defaultPageSize' => 6,
'totalCount' => $query->count(),
]);
$countries = $query->orderBy('name')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
//指向跳转位置页面和携带的参数
return $this->render('index', [
'countries' => $countries,
'pagination' => $pagination,
]);
}
//下面是增加数据库的方法
public function actionAdd(){
$ty = new Country();
$ty->code = 'gd';
$ty->name = 'ceshiname';
$ty->population = '10010';
if(($ty->save())>0){
echo "添加ok";
}else{
echo "fail";
}
}
//下面是删除的方法
public function actionDel(){
$tydel = new Country();
$success = $tydel->deleteAll('name ="ceshiname"');
if($success>0){echo "删除成功"; }else{echo "删除失败"; }
}
//下面是修改方法
public function actionUpdate(){
$tyupdate = new Country();
$kkk = $tyupdate->updateAll(array('code'=>'mH'),'code="CH"');
if($kkk>0){
echo "update success!";
}else{
echo "update fail";
}
}
}
4.建立展现层
位置views/country/index.php
代码如下:
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Countries</h1>
<ul>
<?php foreach ($countries as $country): ?>
<li>
<?= Html::encode("{$country->name} from ({$country->code})") ?>:
<?= $country->population ?>
</li>
<?php endforeach; ?>
</ul>
<?= LinkPager::widget(['pagination' => $pagination]) ?>
说明:name与code都是数据库中存在的字段