From d5eb713888715e8f18d2ccf508a8eb0b1a483ad1 Mon Sep 17 00:00:00 2001
From: wei <>
Date: Tue, 24 Apr 2007 06:14:56 +0000
Subject: add active record Relations
---
.../Relations/TActiveRecordHasMany.php | 96 ++++++++++++++++++++++
1 file changed, 96 insertions(+)
create mode 100644 framework/Data/ActiveRecord/Relations/TActiveRecordHasMany.php
(limited to 'framework/Data/ActiveRecord/Relations/TActiveRecordHasMany.php')
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordHasMany.php b/framework/Data/ActiveRecord/Relations/TActiveRecordHasMany.php
new file mode 100644
index 00000000..27ffd194
--- /dev/null
+++ b/framework/Data/ActiveRecord/Relations/TActiveRecordHasMany.php
@@ -0,0 +1,96 @@
+
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright © 2005-2007 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Data.ActiveRecord.Relations
+ */
+
+/**
+ * Loads base active record relations class.
+ */
+Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordRelation');
+
+/**
+ * Implements TActiveRecord::HAS_MANY relationship between the source object having zero or
+ * more foreign objects. Consider the relationship between a Team and a Player.
+ *
+ * +------+ +--------+
+ * | Team | 1 -----> * | Player |
+ * +------+ +--------+
+ *
+ * Where one team may have 0 or more players and each player belongs to only
+ * one team. We may model Team-Player relationship as active record as follows.
+ *
+ * class TeamRecord extends TActiveRecord
+ * {
+ * const TABLE='team';
+ * public $name; //primary key
+ * public $location;
+ *
+ * public $players=array(); //list of players
+ *
+ * protected static $RELATIONS=array(
+ * 'players' => array(self::HAS_MANY, 'PlayerRecord'));
+ *
+ * public static function finder($className=__CLASS__)
+ * {
+ * return parent::finder($className);
+ * }
+ * }
+ * class PlayerRecord extends TActiveRecord
+ * {
+ * const TABLE='player';
+ * public $player_id; //primary key
+ * public $team_name; //foreign key player.team_name <-> team.name
+ * public $age;
+ *
+ * public static function finder($className=__CLASS__)
+ * {
+ * return parent::finder($className);
+ * }
+ * }
+ *
+ * The $RELATIONS static property of TeamRecord defines that the
+ * property $players has many PlayerRecords.
+ *
+ * The players list may be fetched as follows.
+ *
+ * $team = TeamRecord::finder()->with_players()->findAll();
+ *
+ * The method with_xxx() (where xxx is the relationship property
+ * name, in this case, players) fetchs the corresponding PlayerRecords using
+ * a second query (not by using a join). The with_xxx() accepts the same
+ * arguments as other finder methods of TActiveRecord, e.g. with_player('age < ?', 35).
+ *
+ * @author Wei Zhuo
+ * @version $Id$
+ * @package System.Data.ActiveRecord.Relations
+ * @since 3.1
+ */
+class TActiveRecordHasMany extends TActiveRecordRelation
+{
+ /**
+ * Get the foreign key index values from the results and make calls to the
+ * database to find the corresponding foreign objects.
+ * @param array original results.
+ */
+ protected function collectForeignObjects(&$results)
+ {
+ $fkObject = $this->getContext()->getForeignRecordFinder();
+ $fkeys = $this->findForeignKeys($fkObject, $this->getSourceRecord());
+
+ $properties = array_values($fkeys);
+ $fields = array_keys($fkeys);
+
+ $indexValues = $this->getIndexValues($properties, $results);
+ $fkObjects = $this->findForeignObjects($fields,$indexValues);
+ $this->populateResult($results,$properties,$fkObjects,$fields);
+ }
+}
+
+?>
\ No newline at end of file
--
cgit v1.2.3