From 595a0acc2554da4c4c52f065dc76893c241f2624 Mon Sep 17 00:00:00 2001
From: wei <>
Date: Tue, 1 May 2007 00:27:01 +0000
Subject: Add comments to active record relationships, add semi-colon to js
files.
---
.../Data/ActiveRecord/Exceptions/messages.txt | 3 +-
.../Relations/TActiveRecordHasManyAssociation.php | 18 +++++--
.../ActiveRecord/Relations/TActiveRecordHasOne.php | 55 +++++++++++++++++++++-
.../Relations/TActiveRecordRelationContext.php | 6 +--
4 files changed, 73 insertions(+), 9 deletions(-)
(limited to 'framework/Data')
diff --git a/framework/Data/ActiveRecord/Exceptions/messages.txt b/framework/Data/ActiveRecord/Exceptions/messages.txt
index f9979e12..6c13450d 100644
--- a/framework/Data/ActiveRecord/Exceptions/messages.txt
+++ b/framework/Data/ActiveRecord/Exceptions/messages.txt
@@ -19,4 +19,5 @@ ar_invalid_table = Missing, invalid or no permission for table/view '{0}'.
ar_invalid_finder_class_name = Class name for finder($className) method must not be 'TActiveRecord', you should override the finder() method in your record class or pass in a valid record class name.
ar_invalid_criteria = Invalid criteria object, must be a string or instance of TSqlCriteria.
ar_relations_undefined = Unable to determine Active Record relationships because static array property {0}::${1} is not defined.
-ar_undefined_relation_prop = Unable to find {1}::${2}['{0}'], Active Record relationship definition for property "{0}" not found in entries of {1}::${2}.
\ No newline at end of file
+ar_undefined_relation_prop = Unable to find {1}::${2}['{0}'], Active Record relationship definition for property "{0}" not found in entries of {1}::${2}.
+ar_invalid_relationship = Invalid active record relationship.
\ No newline at end of file
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordHasManyAssociation.php b/framework/Data/ActiveRecord/Relations/TActiveRecordHasManyAssociation.php
index a86fdffd..bb2cc583 100644
--- a/framework/Data/ActiveRecord/Relations/TActiveRecordHasManyAssociation.php
+++ b/framework/Data/ActiveRecord/Relations/TActiveRecordHasManyAssociation.php
@@ -1,4 +1,4 @@
-getContext()->getForeignRecordFinder();
$command = $this->createCommand($criteria, $foreignKeys,$indexValues,$sourceKeys);
$srcProps = array_keys($sourceKeys);
- $type = get_class($finder);
$collections=array();
foreach($command->query() as $row)
{
$hash = $this->getObjectHash($row, $srcProps);
foreach($srcProps as $column)
unset($row[$column]);
- $collections[$hash][] = $finder->populateObject($type,$row);
+ $collections[$hash][] = $this->populateObject($finder, $row);
}
$this->setResultCollection($results, $collections, array_values($sourceKeys));
}
+
+ protected function populateObject($finder, $data)
+ {
+ $registry = $finder->getRecordManager()->getObjectStateRegistry();
+ if(!is_null($obj = $registry->getCachedInstance($data, false)))
+ return $obj;
+ $gateway = $finder->getRecordManager()->getRecordGateway();
+ $type = get_class($finder);
+ $obj = new $type($data);
+ return $registry->addCachedInstance($data,$obj);
+ }
/**
* @param TSqlCriteria
@@ -145,5 +155,5 @@ class TActiveRecordHasManyAssociation extends TActiveRecordRelation
return "INNER JOIN {$refTable} ON ({$joinCondition}) AND {$index}";
}
-}
+}
?>
\ No newline at end of file
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordHasOne.php b/framework/Data/ActiveRecord/Relations/TActiveRecordHasOne.php
index e8c2ccee..4286254b 100644
--- a/framework/Data/ActiveRecord/Relations/TActiveRecordHasOne.php
+++ b/framework/Data/ActiveRecord/Relations/TActiveRecordHasOne.php
@@ -18,10 +18,63 @@ Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordRelation');
/**
* TActiveRecordHasOne models the object relationship that a record (the source object)
* property is an instance of foreign record object having a foreign key
- * related to the source object.
+ * related to the source object. The HAS_ONE relation is very similar to the
+ * HAS_MANY relationship (in fact, it is equivalent in the entities relationship point of view).
*
+ * The difference of HAS_ONE from HAS_MANY is that the foreign object is singular.
+ * That is, HAS_MANY will return a collection of records while HAS_ONE returns the
+ * corresponding record.
*
+ * Consider the entity relationship between a Car and a Engine.
+ *
+ * +-----+ +--------+
+ * | Car | 1 <----- 1 | Engine |
+ * +-----+ +--------+
+ *
+ * Where each engine belongs to only one car, that is, the Engine entity has
+ * a foreign key to the Car's primary key. We may model
+ * Engine-Car object relationship as active record as follows.
+ *
+ * class CarRecord extends TActiveRecord
+ * {
+ * const TABLE='car';
+ * public $car_id; //primary key
+ * public $colour;
+ *
+ * public $engine; //engine foreign object
+ *
+ * protected static $RELATIONS=array(
+ * 'engine' => array(self::HAS_ONE, 'EngineRecord'));
+ *
+ * public static function finder($className=__CLASS__)
+ * {
+ * return parent::finder($className);
+ * }
+ * }
+ * class EngineRecord extends TActiveRecord
+ * {
+ * const TABLE='engine';
+ * public $engine_id;
+ * public $capacity;
+ * public $car_id; //foreign key to cars
*
+ * public static function finder($className=__CLASS__)
+ * {
+ * return parent::finder($className);
+ * }
+ * }
+ *
+ * The $RELATIONS static property of CarRecord defines that the
+ * property $engine that will reference an EngineRecord instance.
+ *
+ * The car record with engine property list may be fetched as follows.
+ *
+ * $cars = CarRecord::finder()->with_engine()->findAll();
+ *
+ * The method with_xxx() (where xxx is the relationship property
+ * name, in this case, engine) fetchs the corresponding EngineRecords 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_engine('capacity < ?', 3.8).
*
* @author Wei Zhuo
* @version $Id$
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php b/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
index a33e105e..033d7638 100644
--- a/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
+++ b/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
@@ -1,4 +1,4 @@
-
\ No newline at end of file
--
cgit v1.2.3