yii2数据库增删改查询操作
简单的说:
1.先配置好数据库
位置在config/db.php
更改如下:
<?php
return [
"class" => "yiidbConnection",
"dsn" => "mysql:host=localhost;dbname=music",
"username" => "root",
"password" => "root",
"charset" => "utf8",
];
2.新建立个models层文件这里叫做
位置:models/Country.php
代码如下:
<?php
namespace appmodels;//所有的model都要继承这个
use yiidbActiveRecord;
class Country extends ActiveRecord
{
}
3.新建controllers层文件
<?php
namespace appcontrollers;
use yiiwebController;
use yiidataPagination; //这个使用来进行分页使用的
use appmodelsCountry; //直接把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 yiihelpersHtml;
use yiiwidgetsLinkPager;
?>
<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" => "yiidbConnection",
"dsn" => "mysql:host=localhost;dbname=music",
"username" => "root",
"password" => "root",
"charset" => "utf8",
];
2.新建立个models层文件这里叫做
位置:models/Country.php
代码如下:
<?php
namespace appmodels;//所有的model都要继承这个
use yiidbActiveRecord;
class Country extends ActiveRecord
{
}
3.新建controllers层文件
<?php
namespace appcontrollers;
use yiiwebController;
use yiidataPagination; //这个使用来进行分页使用的
use appmodelsCountry; //直接把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 yiihelpersHtml;
use yiiwidgetsLinkPager;
?>
<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都是数据库中存在的字段
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。