PHP開發架構Yii Framework教程(27) 資料庫-關聯Active Record樣本

來源:互聯網
上載者:User

我們已經瞭解了怎樣使用 Active Record (AR) 從單個資料表中擷取資料。 在本節中,我們講解怎樣使用 AR 串連多個相關 資料表並取回關聯(join)後的資料集。

為了使用關係型 AR,我們建議在需要關聯的表中定義主鍵-外鍵約束。這些約 束可以協助保證相關資料的一致性和完整性。

本例通過修改Yii Framework 開發教程(25) 資料庫-Query Builder樣本來 介紹多個有關係的表如何使用Active Record。

在我們使用 AR 執行關聯查詢之前,我們需要讓 AR 知道一個 AR 類是怎 樣關聯到另一個的。

兩個 AR 類之間的關係直接通過 AR 類所代表的資料表之間的關係相關聯。 從資料庫的角度來說, 表 A 和 B 之間有三種關係:一對多(one-to-many,例如 tbl_user 和 tbl_post),一對一( one-to-one 例如 tbl_user 和 tbl_profile)和 多對多(many-to-many 例如 tbl_category 和 tbl_post)。 在 AR 中,有四種關係:

BELONGS_TO( 屬於): 如果表 A 和 B 之間的關係是一對多,則 表 B 屬於 表 A (例如 Post 屬於 User);

HAS_MANY(有多個): 如果表 A 和 B 之間的關係是一對多,則 A 有多個 B (例如 User 有多個 Post);

HAS_ONE(有一個): 這是 HAS_MANY 的一個特例 ,A 最多有一個 B (例如 User 最多有一個 Profile);

MANY_MANY: 這個對應於資料庫中的 多對多 關係。 由於多數 DBMS 不直接支援 多對多 關係,因此需要有一個關聯表將 多對多 關係分割為 一對多 關係。 在我們的樣本資料結構中, tbl_post_category 就是用於此目的的。在 AR 術語中,我們可以解釋 MANY_MANY 為 BELONGS_TO 和 HAS_MANY 的組合。 例如 ,Post 屬於多個(belongs to many) Category ,Category 有多個(has many) Post.

AR 中定義關係需要覆蓋 CActiveRecord 中的 relations() 方法。此方法返回一個關係配置數組。每個數組元素通過如下格式表示一個單一的關係。

在Query Builder中我們使用了下面SQL查詢語句

SELECT c.FirstName, c.LastName , c.Address,c.Email     FROM customer c     INNER JOIN employee e     ON c.SupportRepId=e.EmployeeId     WHERE e.EmployeeId=4

涉及到兩個表格Employee 和 Customer,Employee和Customer之間是一對多的關係,也就是說 一個員工可以負責多個客戶。Employee到Customer的關係為HAS_MANY, Customer到Employee的關係為HAS_ONE。因此可以定義 Employee和Customer如下:

//Customer.php     class Customer extends CActiveRecord     {         public static function model($className=__CLASS__)         {             return parent::model($className);         }         public function tableName()         {             return 'Customer';         }     }     //Employee.php     class Employee extends CActiveRecord     {         public static function model($className=__CLASS__)         {             return parent::model($className);         }         public function tableName()         {             return 'Employee';         }         public function relations()         {             return array(                 'customers'=>array(self::HAS_MANY, 'Customer', 'SupportRepId'),                 );         }     }

因為本例只使用到由Employee查詢對應的Customer,因此只為類定義了relations方法。對應的表和外鍵為Customer 和SupportRepId。
然後修改SiteController的indexAction方法:

public function actionIndex()     {         $employee=Employee::model()->findByPk(4);         $this->render('index', array(             'model' => $employee->customers,             ));     }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.