溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

YII2數(shù)據(jù)庫如何查詢

發(fā)布時間:2021-09-01 11:14:39 來源:億速云 閱讀:189 作者:小新 欄目:數(shù)據(jù)庫

小編給大家分享一下YII2數(shù)據(jù)庫如何查詢,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

數(shù)據(jù)庫配置。

/config/db.php 進(jìn)行數(shù)據(jù)庫配置

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

mysql> select * from test;
+----+--------+
| id | name |
+----+--------+
| 1 | zhuai |
| 2 | heng | 
+----+--------+
18 rows in set (0.00 sec)

sql 查詢方式

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

// 最基本的查詢方式
$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方式,都會提供相應(yīng)的封裝的查詢方式,yii2亦然。

創(chuàng)建model

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

<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class Test extends ActiveRecord
{
// 可無,對應(yīng)表:默認(rèn)類名和表名匹配,則無需此函數(shù)
public static function tableName()
{
return 'test';
}
// 可無,驗證器:主要用于校驗各個字段
public function rules(){
return [
['id', 'integer'],
['name', 'string', 'length' => [0, 100]],
];
}
}

使用的時候需要引入model

use app\models\Test;
增加操作
// add 操作
$test = new Test();
$test->name = 'test';
// 合法性校驗
$test->validate();
if($test->hasErrors()){
echo "數(shù)據(jù)不合法";
die;
}
$test->save();

查詢操作

查詢操作先上官方文檔

activeRecord doc

where doc

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

// 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
// 批量查詢,對于大內(nèi)存操作的批量查詢
foreach (Test::find()->batch(1) as $test) {
var_dump(count($test));
}

刪除操作

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

修改操作

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

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

關(guān)聯(lián)查詢操作

關(guān)聯(lián)查詢示例中兩個表:

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

一個分?jǐn)?shù)表(score):id,stu_id,score

// 相應(yīng)學(xué)生的所有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);

兩種關(guān)聯(lián)查詢方式;但是,在controller進(jìn)行相關(guān)操作,代碼顯的過于混亂,在model中封裝調(diào)用

首先在student model中封裝相關(guān)關(guān)聯(lián)調(diào)用函數(shù)

<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class Student extends ActiveRecord
{
public static function tableName()
{
return 'student';
}
// 獲取分?jǐn)?shù)信息
public function getScores()
{
$scores = $this->hasMany(Score::className(), ['stu_id' => 'id'])->asArray()->all();
return $scores;
}
}

之后直接調(diào)用,兩種調(diào)用方式

// 函數(shù)封裝之后調(diào)用
$scores = $stu->getScores();
var_dump($scores);
// 利用__get 的自動調(diào)用的方式
$scores = $stu->scores;
var_dump($scores);

以上是“YII2數(shù)據(jù)庫如何查詢”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI