From be7deeed610871839ba166a5d4c8237e5cdbe864 Mon Sep 17 00:00:00 2001 From: xue <> Date: Tue, 4 Dec 2007 18:59:48 +0000 Subject: Active Record now supports query criteria for implicitly declared related properties --- .../protected/pages/Database/ActiveRecord.page | 60 ++++++++++++++-------- 1 file changed, 40 insertions(+), 20 deletions(-) (limited to 'demos/quickstart/protected/pages/Database') diff --git a/demos/quickstart/protected/pages/Database/ActiveRecord.page b/demos/quickstart/protected/pages/Database/ActiveRecord.page index 28beaa93..95ce96c7 100644 --- a/demos/quickstart/protected/pages/Database/ActiveRecord.page +++ b/demos/quickstart/protected/pages/Database/ActiveRecord.page @@ -612,7 +612,8 @@ CREATE TABLE bar as an 1-M relationship. That is, one Team may contain 0 or more Players. In terms of object relationships, we say that a TeamRecord object has many PlayerRecord objects. (Notice the reversal of the direction of relationships between tables and objects.) -

+

+

Has Many Relationship

We model the Team object as the following Active Record classes. @@ -629,7 +630,7 @@ class TeamRecord extends TActiveRecord //define the $player member having has many relationship with PlayerRecord public static $RELATIONS=array ( - 'players' => array(self::HAS_MANY, 'PlayerRecord'), + 'players' => array(self::HAS_MANY, 'PlayerRecord', 'team_name'), ); public static function finder($className=__CLASS__) @@ -644,10 +645,12 @@ property $players has many PlayerRecords. Multiple rela is permitted by defining each relationship with an entry in the $RELATIONS array where array key for the entry corresponds to the property name. In array(self::HAS_MANY, 'PlayerRecord'), the first element defines the -relationship type, the valid types are self::HAS_MANY, -self::HAS_ONE and self::BELONGS_TO. +relationship type, the valid types are self::HAS_MANY, self::HAS_ONE, +self::BELONGS_TO and self::MANY_TO_MANY. The second element is a string 'PlayerRecord' that corresponds to the class name of the PlayerRecord class. +And the third element 'team_name' refers to the foreign key column in the Players table that +references to the Teams table.

Note: @@ -670,8 +673,8 @@ in Active Record by inspecting the Players and Teams table def Since version 3.1.2, Active Record supports multiple foreign key references of the same table. Ambiguity between multiple foreign key references to the same table is resolved by providing the foreign key column name as the 3rd parameter in the relationship array. -For example, both the following foreign keys owner_id and reporter_id -references the same table defined in UserRecord. +For example, both of the following foreign keys owner_id and reporter_id +references to the same table defined in UserRecord. class TicketRecord extends TActiveRecord { @@ -729,6 +732,13 @@ load the related objects, while the with approach is more efficient if returned, each with some related objects.
+

Has One Relationship

+

The entity relationship between Players and Profiles is one to one. That is, +each PlayerRecord object has one ProfileRecord object (may be none or null). +A has one relationship is nearly identical to a has many relationship with the exception +that the related object is only one object (not a collection of objects). +

+

Belongs To Relationship

The "has many" relationship in the above section defines a collection of foreign objects. In particular, we have that a TeamRecord has many (zero or more) @@ -752,9 +762,9 @@ class PlayerRecord extends TActiveRecord public static $RELATIONS=array ( - 'team' => array(self::BELONGS_TO, 'TeamRecord'), + 'team' => array(self::BELONGS_TO, 'TeamRecord', 'team_name'), 'skills' => array(self::MANY_TO_MANY, 'SkillRecord', 'Player_Skills'), - 'profile' => array(self::HAS_ONE, 'ProfileRecord'), + 'profile' => array(self::HAS_ONE, 'ProfileRecord', 'player_id'), ); public static function finder($className=__CLASS__) @@ -768,10 +778,11 @@ The static $RELATIONS property of PlayerRecord defines that th property $team belongs to a TeamRecord. The $RELATIONS array also defines two other relationships that we shall examine in later sections below. -In array(self::BELONGS_TO, 'TeamRecord'), the first element defines the -relationship type, in this case self::BELONGS_TO and +In array(self::BELONGS_TO, 'TeamRecord', 'team_name'), the first element defines the +relationship type, in this case self::BELONGS_TO; the second element is a string 'TeamRecord' that corresponds to the -class name of the TeamRecord class. +class name of the TeamRecord class; and the third element 'team_name' refers +to the foreign key of Players referencing Teams. A player object with the corresponding team object may be fetched as follows.

@@ -830,13 +841,6 @@ Thus, the PlayerRecord object has a property ($team) that b TeamRecord object.

-

Has One Relationship

-

The entity relationship between Players and Profiles is one to one. That is, -each PlayerRecord object has one ProfileRecord object (may be none or null). -A has one relationship is nearly identical to a has many relationship with the exception -that the related object is only one object (not a collection of objects). -

-

Parent Child Relationships

A parent child relationship can be defined using a combination of has many and belongs to relationship that refers to the same class. The following example shows a parent children relationship between @@ -855,12 +859,28 @@ class Category extends TActiveRecord public static $RELATIONS=array ( - 'parent_category' => array(self::BELONGS_TO, 'Category'), - 'child_categories' => array(self::HAS_MANY, 'Category'), + 'parent_category' => array(self::BELONGS_TO, 'Category', 'parent_cat_id'), + 'child_categories' => array(self::HAS_MANY, 'Category', 'parent_cat_id'), ); } +

Query Criteria for Related Objects

+

+In the above, we show that an Active Record object can reference to its related objects by +declaring a static class member $RELATIONS which specifies a list of relations. Each relation +is specified as an array consisting of three elements: relation type, related AR class name, +and the foreign key(s). For example, we use array(self::HAS_MANY, 'PlayerRecord', 'team_name') +to specify the players in a team. There are two more optional elements that can be specified +in this array: query condition (the fourth element) and parameters (the fifth element). +They are used to control how to query for the related objects. For example, if we want to obtain +the players ordered by their age, we can specify array(self::HAS_MANY, 'PlayerRecord', 'team_name', 'ORDER BY age'). +If we want to obtain players whose age is smaller than 30, we could use +array(self::HAS_MANY, 'PlayerRecord', 'team_name', 'age<:age', array(':age'=>30)). In general, +these two additional elements are similar as the parameters passed to the find() method in AR. +

+ +

Association Table Mapping

-- cgit v1.2.3