yii框架數(shù)據(jù)庫關(guān)聯(lián)查詢操作示例
本文實(shí)例講述了yii框架數(shù)據(jù)庫關(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模型沒有具體方法。
上邊的 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; //說明,當(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ù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- PHP的Yii框架中使用數(shù)據(jù)庫的配置和SQL操作實(shí)例教程
- Yii2.0高級(jí)框架數(shù)據(jù)庫增刪改查的一些操作
- Yii2——使用數(shù)據(jù)庫操作匯總(增刪查改、事務(wù))
- Yii2框架實(shí)現(xiàn)數(shù)據(jù)庫常用操作總結(jié)
- Yii2框架操作數(shù)據(jù)庫的方法分析【以mysql為例】
- Yii框架實(shí)現(xiàn)對(duì)數(shù)據(jù)庫的CURD操作示例
- Yii框架數(shù)據(jù)庫查詢、增加、刪除操作示例
- 解析yii數(shù)據(jù)庫的增刪查改
- Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結(jié)
- Yii框架自定義數(shù)據(jù)庫操作組件示例
相關(guān)文章
PHP中幾個(gè)可以提高運(yùn)行效率的代碼寫法、技巧分享
這篇文章主要介紹了PHP中幾個(gè)可以提高運(yùn)行效率的代碼寫法、技巧分享,本文分享的5個(gè)方法都是在細(xì)微之處有所不同,一定要認(rèn)真仔細(xì)的去看代碼哦,需要的朋友可以參考下2014-08-08PHP實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)算法分享
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(六)
這篇文章主要介紹了PHP網(wǎng)頁游戲Xnova(ogame)源碼解讀的公共代碼,需要的朋友可以參考下2014-06-06thinkPHP+ajax實(shí)現(xiàn)統(tǒng)計(jì)頁面pv瀏覽量的方法
這篇文章主要介紹了thinkPHP+ajax實(shí)現(xiàn)統(tǒng)計(jì)頁面pv瀏覽量的方法,涉及thinkPHP模板調(diào)用及數(shù)據(jù)庫讀寫相關(guān)操作技巧,需要的朋友可以參考下2017-03-03Yii2中使用join、joinwith多表關(guān)聯(lián)查詢
這篇文章主要介紹了Yii2中多表關(guān)聯(lián)查詢(join、joinwith)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06thinkPHP3.2使用RBAC實(shí)現(xiàn)權(quán)限管理的實(shí)現(xiàn)
這篇文章主要介紹了thinkPHP3.2使用RBAC實(shí)現(xiàn)權(quán)限管理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08