《PHP應用:Yii2.0表關聯(lián)查詢實例分析》要點:
本文介紹了PHP應用:Yii2.0表關聯(lián)查詢實例分析,希望對您有用。如果有疑問,可以聯(lián)系我們。
相關主題:YII框架
PHP編程本文實例講述了Yii2.0表關聯(lián)查詢的方法.分享給大家供大家參考,具體如下:
PHP編程你可以使用 ActiveRecord 來進行關聯(lián)查詢(比如,從A表讀取數(shù)據(jù)時把關聯(lián)的B表數(shù)據(jù)也一起讀出來), 在Active Record中,獲取關聯(lián)數(shù)據(jù)可以像訪問主表ActiveRecord對象的屬性(property)一樣簡單.
PHP編程比如,通過合適的關系聲明,你可以使用 $customer->orders 來獲取一個 Order 對象數(shù)組,代表該客戶下的訂單.
PHP編程要聲明一個關系(relation),定義一個getter方法,該方法返回一個 yii\db\ActiveQuery 對象,擁有關聯(lián)上下文信息,這樣將只查詢符合條件的相關數(shù)據(jù).比如:
PHP編程
class Customer extends \yii\db\ActiveRecord
{
public function getOrders()
{
// Customer has_many Order via Order.customer_id -> id
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
}
}
class Order extends \yii\db\ActiveRecord
{
// Order has_one Customer via Customer.id -> customer_id
public function getCustomer()
{
return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
}
}
PHP編程上述代碼中的 yii\db\ActiveRecord::hasMany() 和 yii\db\ActiveRecord::hasOne() 是用來建模關系型數(shù)據(jù)庫中的 一對多 以及 一對一 關聯(lián)關系.比如,一個客戶customer有多個訂單orders,而一個訂單擁有或歸屬于一個用戶.兩個方法均接收兩個參數(shù)并返回一個 yii\db\ActiveQuery 對象:
PHP編程$class: 關聯(lián)模型的類名稱.
PHP編程$link: 兩張表之間的列關聯(lián).這得是一個數(shù)組.數(shù)組元素的鍵是 $class 所對應表的列名稱,而數(shù)組元素的值是當前聲明類的列名稱.依據(jù)表外鍵關聯(lián)來定義這些關系是一個好的編程實踐.
PHP編程完成上述聲明后,就可以通過定義相應的getter方法來像訪問對象屬性一樣獲取關聯(lián)數(shù)據(jù):
PHP編程
// get the orders of a customer
$customer = Customer::findOne(1);
$orders = $customer->orders; // $orders is an array of Order objects
PHP編程上述代碼在幕后實際執(zhí)行了如下兩個SQL查詢,分別對應于上述兩行代碼:
PHP編程
SELECT * FROM customer WHERE id=1;
SELECT * FROM order WHERE customer_id=1;
PHP編程提示:如果你再次訪問 $customer->orders ,并不會重復執(zhí)行上述第2行SQL查詢.這條查詢語句只在表達式第一次被訪問時才被執(zhí)行.后續(xù)的訪問將直接返回內部緩沖數(shù)據(jù).如果你想重新執(zhí)行查詢,只需要先調用一下unset來清除緩存:
PHP編程
unset($customer->orders);.
PHP編程有時候,你可能想傳遞參數(shù)給關聯(lián)查詢來限定查詢條件.比如只想讀取超過指定數(shù)額的大額訂單,而不是所有訂單.為此,可以使用如下getter方法來聲明一個 bigOrders 關系:
PHP編程
class Customer extends \yii\db\ActiveRecord
{
public function getBigOrders($threshold = 100)
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])
->where('subtotal > :threshold', [':threshold' => $threshold])
->orderBy('id');
}
}
PHP編程記住 hasMany() 返回對象是一個 yii\db\ActiveQuery ,因此ActiveQuery的方法都可以被用來定制這個關聯(lián)查詢.
PHP編程通過上述聲明,如果你訪問 $customer->bigOrders, 它將只返回數(shù)額大于100的訂單.如果想指定一個不同的限定值,使用如下代碼:
PHP編程
$orders = $customer->getBigOrders(200)->all();
PHP編程注意:關聯(lián)方法返回一個 yii\db\ActiveQuery 實例.如果你以屬性(類property)的方式來訪問它,返回數(shù)據(jù)是一個 yii\db\ActiveRecord 實例、或者是ActiveRecord數(shù)組或為空(null).比如, $customer->getOrders() 返回一個 ActiveQuery 實例,而$customer->orders 返回一個 Order 對象數(shù)組(或者是一個空數(shù)組,如果查詢結果為空).
PHP編程中間表關聯(lián)查詢
PHP編程有時候,一些數(shù)據(jù)表通過中間表(pivot table)關聯(lián)在一起.為了聲明這樣的關系,我們可以定制 yii\db\ActiveQuery 對象,通過調用它的 via() 或 viaTable() 方法.
PHP編程比如,如果訂單表 order 和商品表 item 通過連接表 order_item關聯(lián),我們可以在 Order 類中聲明 items 關系如下:
PHP編程
class Order extends \yii\db\ActiveRecord
{
public function getItems()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->viaTable('order_item', ['order_id' => 'id']);
}
}
PHP編程via() 方法和 viaTable() 類似,不過第一個參數(shù)是在當前ActiveRecord類中聲明的一個關系(relation)名,而不是中間表的名稱.比如,上述 items 關系也可以用下面的方法進行聲明:
PHP編程
class Order extends \yii\db\ActiveRecord
{
public function getOrderItems()
{
return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
}
public function getItems()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems');
}
}
PHP編程更多關于Yii相關內容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優(yōu)秀開發(fā)框架總結》、《smarty模板入門基礎教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
PHP編程希望本文所述對大家基于Yii框架的PHP程序設計有所幫助.
轉載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/5636.html