本文實(shí)例講述了yii框架數(shù)據(jù)庫(kù)關(guān)聯(lián)查詢操作。分享給大家供大家參考,具體如下:
?php namespace app\controllers; use yii\web\Controller; use app\models\Customer; class CustomerController extends Controller{ //根據(jù)顧客名字查詢出所有的訂單信息 public function actionIndex(){ $customer = Customer::find()->where(['name'=>'zhangsan'])->one(); $orders = $customer->hasMany('app\models\Order',['customer_id'=>'id'])->asArray()->all(); print_r($orders); } } ?>
上邊的控制器方法查詢,Customer模型沒(méi)有具體方法。
上邊的 app\models\Order 可以改進(jìn)為Order::className()
,并且上邊要添加use app\models\Order;
方式二:(使用model方法)
customer模型代碼:
?php namespace app\models; use yii\db\ActiveRecord; class Customer extends ActiveRecord{ public function getOrders(){ return $this->hasMany(Order::className(),['customer_id'=>'id'])->asArray()->all(); } }
控制器代碼:
namespace app\controllers; use yii\web\Controller; use app\models\Customer; class CustomerController extends Controller{ //根據(jù)顧客名字查詢出所有的訂單信息 public function actionIndex(){ $customer = Customer::find()->where(['name'=>'zhangsan'])->one(); $orders = $customer->getOrders(); print_r($orders); } }
方法三:(調(diào)用模型的屬性查詢)
customer模型代碼:
namespace app\models; use yii\db\ActiveRecord; class Customer extends ActiveRecord{ public function getOrders(){ return $this->hasMany(Order::className(),['customer_id'=>'id'])->asArray(); } }
控制器代碼:
namespace app\controllers; use yii\web\Controller; use app\models\Customer; class CustomerController extends Controller{ //根據(jù)顧客名字查詢出所有的訂單信息 public function actionIndex(){ $customer = Customer::find()->where(['name'=>'zhangsan'])->one(); $orders = $customer->orders; //說(shuō)明,當(dāng)調(diào)用一個(gè)不存在的屬性時(shí), //php會(huì)去調(diào)用一個(gè)__get()的方法, //__get()的方法會(huì)自動(dòng)調(diào)用一個(gè)get+屬性的方法,即getOrders() //并且會(huì)再查詢時(shí)自動(dòng)補(bǔ)上->all()或->one()方法,根據(jù)模型查詢的hasMany或hasOne決定的 print_r($orders); } }
根據(jù)訂單id獲取對(duì)應(yīng)的顧客信息:
模型代碼:
namespace app\models; use yii\db\ActiveRecord; class Order extends ActiveRecord{ //根據(jù)訂單id獲取顧客信息 public function getCustomer(){ return $this->hasOne(Customer::className(),['id'=>'customer_id'])->asArray(); } }
控制器代碼:
namespace app\controllers; use yii\web\Controller; use app\models\Order; class CustomerController extends Controller{ //根據(jù)訂單查詢用戶信息 public function actionIndex(){ $orders = Order::find()->where(['id'=>2])->one(); $customer = $orders->customer; print_r($customer); } }
以上代碼中的$orders->customer
會(huì)記錄緩存,如果要?jiǎng)h除緩存,可以使用unset($orders->customer)
。
關(guān)聯(lián)查詢的多次查詢
$customers = Customer::find()->all(); foreach($customers as $customer){ $orders = $customer->orders; }
這樣如果有100條數(shù)據(jù),就總共需要查詢101次。
優(yōu)化:
$customers = Customer::find()->with('orders')->all(); foreach($customers as $customer){ $orders = $customer->orders; }
總共查詢兩次。
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
標(biāo)簽:郴州 通化 自貢 佳木斯 金華 阿克蘇 香港 寶雞
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《yii框架數(shù)據(jù)庫(kù)關(guān)聯(lián)查詢操作示例》,本文關(guān)鍵詞 yii,框架,數(shù)據(jù)庫(kù),關(guān)聯(lián),查詢,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。