一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數據庫技術|

服務器之家 - 數據庫 - 數據庫技術 - YII2數據庫查詢實踐

YII2數據庫查詢實踐

2021-10-26 16:1999re 數據庫技術

這篇文章主要介紹了YII2數據庫查詢實踐的相關資料,需要的朋友可以參考下

初探yii2框架,對增刪改查,關聯查詢等數據庫基本操作的簡單實踐。

數據庫配置。

/config/db.php 進行數據庫配置

實踐過程中有個test庫-》test表-》兩條記錄如下

?
1
2
3
4
5
6
7
8
mysql> select * from test;
+----+--------+
| id | name |
+----+--------+
| 1 | zhuai |
| 2 | heng |
+----+--------+
18 rows in set (0.00 sec)

sql 查詢方式

yii2 提供了原始的數據庫查詢方式findBySql;同時, 通過占位符的方式,自動進行了基本的sql注入防御 。上碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
// 最基本的查詢方式
$sql = "select * from test where 1";
$res = Test::findBySql($sql)->all();
var_dump(count($res)); // res->2
// findbysql 防止sql注入方式
$id = '1 or 1=1';
$sql = "select * from test where id = " . $id;
$res = Test::findBySql($sql)->all();
var_dump(count($res)); // res-> 2
$sql = "select * from test where id = :id";
// 定位符會自動防止sql 注入
$res = Test::findBySql($sql,array(":id"=>$id))->all();
var_dump(count($res)); // res->1

activeRecord查詢方式

每個框架除了原有的sql方式,都會提供相應的封裝的查詢方式,yii2亦然。

創建model

yii的model基本方式如下,代碼如下不贅述。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class Test extends ActiveRecord
{
// 可無,對應表:默認類名和表名匹配,則無需此函數
public static function tableName()
{
return 'test';
}
// 可無,驗證器:主要用于校驗各個字段
public function rules(){
return [
['id', 'integer'],
['name', 'string', 'length' => [0, 100]],
];
}
}

使用的時候需要引入model

?
1
2
3
4
5
6
7
8
9
10
11
12
use app\models\Test;
增加操作
// add 操作
$test = new Test();
$test->name = 'test';
// 合法性校驗
$test->validate();
if($test->hasErrors()){
echo "數據不合法";
die;
}
$test->save();

查詢操作

查詢操作先上官方文檔

activeRecord doc

where doc

需要強調的是:yii查詢提供了特別多豐富的庫,例如代碼中的批量查詢處理等等,細節可以看文檔。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// select
// id = 1
$res = Test::find()->where(['id' => 1])->all();
var_dump(count($res)); //1
// id > 0
$res = Test::find()->where(['>','id',0])->all();
var_dump(count($res)); //2
// id > =1 id <=2
$res = Test::find()->where(['between','id',1,2])->all();
var_dump(count($res)); //2
// name字段like
$res = Test::find()->where(['like', 'name', 'cuihuan'])->all();
var_dump(count($res)); //2
// 查詢的使用 obj->array
$res = Test::find()->where(['between','id',1,2])->asArray()->all();
var_dump($res[0]['id']); //2
// 批量查詢,對于大內存操作的批量查詢
foreach (Test::find()->batch(1) as $test) {
var_dump(count($test));
}

刪除操作

?
1
2
3
4
5
6
// delete
// 選出來刪除
$res = Test::find()->where(['id'=>1])->all();
$res[0]->delete();
// 直接刪除
var_dump(Test::deleteAll('id>:id', array(':id' => 2)));

修改操作

除了代碼中方式,yii2直接提供update操作。

?
1
2
3
4
// 活動記錄修改
$res = Test::find()->where(['id'=>4])->one();
$res->name = "update";
$res->save();

關聯查詢操作

關聯查詢示例中兩個表:

一個學生表(student):id ,name;

一個分數表(score):id,stu_id,score

?
1
2
3
4
5
6
7
8
// 相應學生的所有score
$stu = Student::find()->where(['name'=>'xiaozhuai'])->one();
var_dump($stu->id);
// 基本獲取
$scores_1 = $stu->hasMany('app\model\Score',['stu_id'=>$stu->id])->asArray()->all();
$scores_2 = $stu->hasMany(Score::className(),['stu_id'=>'id'])->asArray()->all();
var_dump($scores_1);
var_dump($scores_2);

兩種關聯查詢方式;但是,在controller進行相關操作,代碼顯的過于混亂,在model中封裝調用

首先在student model中封裝相關關聯調用函數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class Student extends ActiveRecord
{
public static function tableName()
{
return 'student';
}
// 獲取分數信息
public function getScores()
{
$scores = $this->hasMany(Score::className(), ['stu_id' => 'id'])->asArray()->all();
return $scores;
}
}

之后直接調用,兩種調用方式

?
1
2
3
4
5
6
// 函數封裝之后調用
$scores = $stu->getScores();
var_dump($scores);
// 利用__get 的自動調用的方式
$scores = $stu->scores;
var_dump($scores);

最后

上面在yii2的部署和使用過程中的一些基本的增刪改查,關聯查詢等操作。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产三级自拍 | 思思久久精品在热线热 | 成年人免费看的视频 | 亚洲国产天堂综合一区 | 美女被躁爽死 | 99视频在线观看视频 | 韩国情事伦理片观看地址 | 免费超级乱淫视频播放性 | b站免费网站入口 | 日本大片免aaa费观看视频 | 亚洲视频在线观看地址 | 色综合久久中文字幕网 | 国产麻豆流白浆在线观看 | 亚洲精品午夜久久aaa级久久久 | 亚洲国产精品综合福利专区 | 99re这里只有精品视频在线观看 | 婷婷综合久久 | 波多野结衣同性系列698 | 四虎影院免费在线播放 | 亚洲AV 中文字幕 国产 欧美 | blackedhd 18sex| 久久re6热在线视频 久久AV喷吹AV高潮欧美 | 91真人毛片一级在线播放 | 把女的下面扒开添视频 | 男女乱淫真视频播放网站 | 日本www色视频成人免费 | 国产自拍啪啪 | 九色PORNY真实丨国产免费 | 久久精品黄AA片一区二区三区 | 美女张开大腿让男人桶 | 国产综合久久久久 | 日本高清免费不卡在线播放 | 黑帮少爷爱上我第8集在线观看 | 国产精品永久免费自在线观看 | 精品一区二区三区视频日产 | 国产成年人在线观看 | 四虎影院在线免费播放 | 欧美性bbbbbxxxxxxx | 幸福草电视剧演员表介绍 | 男人疯狂进女人下部视频动漫 | 亚洲AV久久无码精品蜜桃 |