本文實例講述了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 可以改進為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)用一個不存在的屬性時,
//php會去調(diào)用一個__get()的方法,
//__get()的方法會自動調(diào)用一個get+屬性的方法,即getOrders()
//并且會再查詢時自動補上->all()或->one()方法,根據(jù)模型查詢的hasMany或hasOne決定的
print_r($orders);
}
}
根據(jù)訂單id獲取對應(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
會記錄緩存,如果要刪除緩存,可以使用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è)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- PHP的Yii框架中使用數(shù)據(jù)庫的配置和SQL操作實例教程
- Yii2.0高級框架數(shù)據(jù)庫增刪改查的一些操作
- Yii2——使用數(shù)據(jù)庫操作匯總(增刪查改、事務(wù))
- Yii2框架實現(xiàn)數(shù)據(jù)庫常用操作總結(jié)
- Yii2框架操作數(shù)據(jù)庫的方法分析【以mysql為例】
- Yii框架實現(xiàn)對數(shù)據(jù)庫的CURD操作示例
- Yii框架數(shù)據(jù)庫查詢、增加、刪除操作示例
- 解析yii數(shù)據(jù)庫的增刪查改
- Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結(jié)
- Yii框架自定義數(shù)據(jù)庫操作組件示例