From 4ceba82b9863f2c6323cbe00407e4bfbedbfc1cd Mon Sep 17 00:00:00 2001 From: wei <> Date: Mon, 8 Oct 2007 03:24:07 +0000 Subject: Allow active records to have multiple foreign key references to the same table. Add TXCache. --- .../protected/pages/Database/ActiveRecord.page | 52 ++++++++++++++++----- .../protected/pages/Database/ar_objects.png | Bin 19837 -> 20638 bytes .../protected/pages/Database/ar_objects.vsd | Bin 190976 -> 190976 bytes .../protected/pages/Database/id/ActiveRecord.page | 8 ++-- 4 files changed, 44 insertions(+), 16 deletions(-) (limited to 'demos/quickstart/protected') diff --git a/demos/quickstart/protected/pages/Database/ActiveRecord.page b/demos/quickstart/protected/pages/Database/ActiveRecord.page index a6e087b9..cb10d184 100644 --- a/demos/quickstart/protected/pages/Database/ActiveRecord.page +++ b/demos/quickstart/protected/pages/Database/ActiveRecord.page @@ -657,10 +657,30 @@ in Active Record by inspecting the Players and Teams table def

Info: -Active Record supports multiple table foreign key relationships with the restriction -that each relationship corresponds to a unique table. For example, the Players -table may only have one set of foreign key relationship with table Teams, it may -have other relationships that corresponds to other tables (including the Players table itself). +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. + +class TicketRecord extends TActiveRecord +{ + public $owner_id; + public $reporter_id; + + public $owner; + public $reporter; + + public static $RELATION=array + ( + 'owner' => array(self::BELONGS_TO, 'UserRecord', 'owner_id'), + 'reporter' => array(self::BELONGS_TO, 'UserRecord', 'reporter_id'), + ); +} + +This is applicable to relationships including BELONGS_TO, HAS_ONE and +HAS_MANY. See section Self Referenced Association Tables for solving ambiguity of MANY_TO_MANY +relationships.

The "has many" relationship is not fetched automatically when you use any of the Active Record finder methods. @@ -713,7 +733,7 @@ class PlayerRecord extends TActiveRecord public static $RELATIONS=array ( 'team' => array(self::BELONGS_TO, 'TeamRecord'), - 'skills' => array(self::HAS_MANY, 'SkillRecord', 'Player_Skills'), + 'skills' => array(self::MANY_TO_MANY, 'SkillRecord', 'Player_Skills'), 'profile' => array(self::HAS_ONE, 'ProfileRecord'), ); @@ -850,9 +870,9 @@ in the Player_Skills association table using an inner join.

The Prado Active Record design implements the two stage approach. For the -Players-Skills M-N (many-to-many) entity relationship, we need -to define a has many relationship in the PlayerRecord class and -in addition define a has many relationship in the SkillRecord class as well. +Players-Skills M-N (many-to-many) entity relationship, we +define a many-to-many relationship in the PlayerRecord class and +in addition we may define a many-to-many relationship in the SkillRecord class as well. The following sample code defines the complete SkillRecord class with a many-to-many relationship with the PlayerRecord class. (See the PlayerRecord class definition above to the corresponding many-to-many relationship with the SkillRecord class.) @@ -869,7 +889,7 @@ class SkillRecord extends TActiveRecord public static $RELATIONS=array ( - 'players' => array(self::HAS_MANY, 'PlayerRecord', 'Player_Skills'), + 'players' => array(self::MANY_TO_MANY, 'PlayerRecord', 'Player_Skills'), ); public static function finder($className=__CLASS__) @@ -882,12 +902,20 @@ class SkillRecord extends TActiveRecord

The static $RELATIONS property of SkillRecord defines that the property $players has many PlayerRecords via an association table 'Player_Skills'. -In array(self::HAS_MANY, 'PlayerRecord', 'Player_Skills'), the first element defines the -relationship type, in this case self::HAS_MANY, +In array(self::MANY_TO_MANY, 'PlayerRecord', 'Player_Skills'), the first element defines the +relationship type, in this case 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 is the name of the association table name.

+ +
Note: +Prior to version 3.1.2 (versions up to 3.1.1), the many-to-many relationship was +defined using self::HAS_MANY. For version 3.1.2 onwards, this must be changed +to self::MANY_TO_MANY. This can be done by searching for the HAS_MANY in your +source code and carfully changing the appropriate definitions. +
+

A list of player objects with the corresponding collection of skill objects may be fetched as follows.

@@ -951,7 +979,7 @@ class Item extends TActiveRecord public static $RELATIONS=array ( - 'related_items' => array(self::HAS_MANY, + 'related_items' => array(self::MANY_TO_MANY, 'Item', 'related_items.related_item_id'), ); } diff --git a/demos/quickstart/protected/pages/Database/ar_objects.png b/demos/quickstart/protected/pages/Database/ar_objects.png index 50ab812d..ac33b88b 100644 Binary files a/demos/quickstart/protected/pages/Database/ar_objects.png and b/demos/quickstart/protected/pages/Database/ar_objects.png differ diff --git a/demos/quickstart/protected/pages/Database/ar_objects.vsd b/demos/quickstart/protected/pages/Database/ar_objects.vsd index 10346c54..d3b3963d 100644 Binary files a/demos/quickstart/protected/pages/Database/ar_objects.vsd and b/demos/quickstart/protected/pages/Database/ar_objects.vsd differ diff --git a/demos/quickstart/protected/pages/Database/id/ActiveRecord.page b/demos/quickstart/protected/pages/Database/id/ActiveRecord.page index b7b9e612..7273640d 100644 --- a/demos/quickstart/protected/pages/Database/id/ActiveRecord.page +++ b/demos/quickstart/protected/pages/Database/id/ActiveRecord.page @@ -615,7 +615,7 @@ class PlayerRecord extends TActiveRecord public static $RELATIONS=array ( 'team' => array(self::BELONGS_TO, 'TeamRecord'), - 'skills' => array(self::HAS_MANY, 'SkillRecord', 'Player_Skills'), + 'skills' => array(self::MANY_TO_MANY, 'SkillRecord', 'Player_Skills'), 'profile' => array(self::HAS_ONE, 'ProfileRecord'), ); @@ -737,7 +737,7 @@ class SkillRecord extends TActiveRecord public static $RELATIONS=array ( - 'players' => array(self::HAS_MANY, 'PlayerRecord', 'Player_Skills'), + 'players' => array(self::MANY_TO_MANY, 'PlayerRecord', 'Player_Skills'), ); public static function finder($className=__CLASS__) @@ -749,7 +749,7 @@ class SkillRecord extends TActiveRecord

Properti statis $RELATIONS dari SkillRecord mendefinisikan bahwa properti $players memiliki banyak PlayerRecords melalui tabel asosiasi 'Player_Skills'. -Dalam array(self::HAS_MANY, 'PlayerRecord', 'Player_Skills'), elemen pertama mendefinisikan tipe hubungan, dalam hal ini self::HAS_MANY, +Dalam array(self::MANY_TO_MANY, 'PlayerRecord', 'Player_Skills'), elemen pertama mendefinisikan tipe hubungan, dalam hal ini self::HAS_MANY, elemen kedua adalah string 'PlayerRecord' yang terkait ke nama kelas dari kelas PlayerRecord, dan elemen ketiga adalah nama dari nama tabel asosiasi.

@@ -805,7 +805,7 @@ class Item extends TActiveRecord public static $RELATIONS=array ( - 'related_items' => array(self::HAS_MANY, + 'related_items' => array(self::MANY_TO_MANY, 'Item', 'related_items.related_item_id'), ); } -- cgit v1.2.3