yii框架數(shù)據(jù)庫關聯(lián)查詢操作示例
本文實例講述了yii框架數(shù)據(jù)庫關聯(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 可以改進為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; //說明,當調(diào)用一個不存在的屬性時, //php會去調(diào)用一個__get()的方法, //__get()的方法會自動調(diào)用一個get+屬性的方法,即getOrders() //并且會再查詢時自動補上->all()或->one()方法,根據(jù)模型查詢的hasMany或hasOne決定的 print_r($orders); } }
根據(jù)訂單id獲取對應的顧客信息:
模型代碼:
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
會記錄緩存,如果要刪除緩存,可以使用unset($orders->customer)
。
關聯(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; }
總共查詢兩次。
更多關于Yii相關內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優(yōu)秀開發(fā)框架總結》、《smarty模板入門基礎教程》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。
- PHP的Yii框架中使用數(shù)據(jù)庫的配置和SQL操作實例教程
- Yii2.0高級框架數(shù)據(jù)庫增刪改查的一些操作
- Yii2——使用數(shù)據(jù)庫操作匯總(增刪查改、事務)
- Yii2框架實現(xiàn)數(shù)據(jù)庫常用操作總結
- Yii2框架操作數(shù)據(jù)庫的方法分析【以mysql為例】
- Yii框架實現(xiàn)對數(shù)據(jù)庫的CURD操作示例
- Yii框架數(shù)據(jù)庫查詢、增加、刪除操作示例
- 解析yii數(shù)據(jù)庫的增刪查改
- Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結
- Yii框架自定義數(shù)據(jù)庫操作組件示例
相關文章
PHP網(wǎng)頁游戲?qū)W習之Xnova(ogame)源碼解讀(六)
這篇文章主要介紹了PHP網(wǎng)頁游戲Xnova(ogame)源碼解讀的公共代碼,需要的朋友可以參考下2014-06-06thinkPHP+ajax實現(xiàn)統(tǒng)計頁面pv瀏覽量的方法
這篇文章主要介紹了thinkPHP+ajax實現(xiàn)統(tǒng)計頁面pv瀏覽量的方法,涉及thinkPHP模板調(diào)用及數(shù)據(jù)庫讀寫相關操作技巧,需要的朋友可以參考下2017-03-03Yii2中使用join、joinwith多表關聯(lián)查詢
這篇文章主要介紹了Yii2中多表關聯(lián)查詢(join、joinwith)的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06thinkPHP3.2使用RBAC實現(xiàn)權限管理的實現(xiàn)
這篇文章主要介紹了thinkPHP3.2使用RBAC實現(xiàn)權限管理的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08