summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
diff options
context:
space:
mode:
authorwei <>2007-10-08 03:24:07 +0000
committerwei <>2007-10-08 03:24:07 +0000
commit4ceba82b9863f2c6323cbe00407e4bfbedbfc1cd (patch)
treebc93498b5bf77b25c5a2d5a87ffaa4f8a9b70728 /framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
parentaedbfe04daaaa0baa8e97c4d7d13e8a1f9d867cb (diff)
Allow active records to have multiple foreign key references to the same table. Add TXCache.
Diffstat (limited to 'framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php')
-rw-r--r--framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php91
1 files changed, 62 insertions, 29 deletions
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php b/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
index 189d2c5e..d83aa63a 100644
--- a/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
+++ b/framework/Data/ActiveRecord/Relations/TActiveRecordRelationContext.php
@@ -32,13 +32,12 @@ class TActiveRecordRelationContext
private $_property;
private $_sourceRecord;
- private $_criteria;
- private $_relation;
+ private $_relation; //data from an entry of TActiveRecord::$RELATION
+ private $_fkeys;
- public function __construct($source, $property=null, $criteria=null)
+ public function __construct($source, $property=null)
{
$this->_sourceRecord=$source;
- $this->_criteria=$criteria;
if($property!==null)
list($this->_property, $this->_relation) = $this->getSourceRecordRelation($property);
}
@@ -48,7 +47,6 @@ class TActiveRecordRelationContext
* property from the $RELATIONS static property in TActiveRecord.
* @param string relation property name
* @return array array($propertyName, $relation) relation definition.
- * @throws TActiveRecordException if property is not defined or missing.
*/
protected function getSourceRecordRelation($property)
{
@@ -58,8 +56,6 @@ class TActiveRecordRelationContext
if(strtolower($name)===$property)
return array($name, $relation);
}
- throw new TActiveRecordException('ar_undefined_relation_prop',
- $property, get_class($this->_sourceRecord), self::RELATIONS_CONST);
}
/**
@@ -71,6 +67,15 @@ class TActiveRecordRelationContext
return $class->getStaticPropertyValue(self::RELATIONS_CONST);
}
+ /**
+ * @return boolean true if the relation is defined in TActiveRecord::$RELATIONS
+ * @since 3.1.2
+ */
+ public function hasRecordRelation()
+ {
+ return $this->_relation!==null;
+ }
+
public function getPropertyValue()
{
$obj = $this->getSourceRecord();
@@ -86,14 +91,6 @@ class TActiveRecordRelationContext
}
/**
- * @return TActiveRecordCriteria sql query criteria for fetching the related record.
- */
- public function getCriteria()
- {
- return $this->_criteria;
- }
-
- /**
* @return TActiveRecord the active record instance that queried for its related records.
*/
public function getSourceRecord()
@@ -110,6 +107,18 @@ class TActiveRecordRelationContext
}
/**
+ * @return array foreign key of this relations, the keys is dependent on the
+ * relationship type.
+ * @since 3.1.2
+ */
+ public function getRelationForeignKeys()
+ {
+ if($this->_fkeys===null)
+ $this->_fkeys=$this->getRelationHandler()->getRelationForeignKeys();
+ return $this->_fkeys;
+ }
+
+ /**
* @return string HAS_MANY, HAS_ONE, or BELONGS_TO
*/
public function getRelationType()
@@ -118,6 +127,25 @@ class TActiveRecordRelationContext
}
/**
+ * @return string foreign key field names, comma delimited.
+ * @since 3.1.2
+ */
+ public function getFkField()
+ {
+ return $this->_relation[2];
+ }
+
+ /**
+ * @return boolean true if the 3rd element of an TActiveRecord::$RELATION entry is set.
+ * @since 3.1.2
+ */
+ public function hasFkField()
+ {
+ $notManyToMany = $this->getRelationType() !== TActiveRecord::MANY_TO_MANY;
+ return $notManyToMany && isset($this->_relation[2]) && !empty($this->_relation[2]);
+ }
+
+ /**
* @return string the M-N relationship association table name.
*/
public function getAssociationTable()
@@ -130,7 +158,8 @@ class TActiveRecordRelationContext
*/
public function hasAssociationTable()
{
- return isset($this->_relation[2]);
+ $isManyToMany = $this->getRelationType() === TActiveRecord::MANY_TO_MANY;
+ return $isManyToMany && isset($this->_relation[2]) && !empty($this->_relation[2]);
}
/**
@@ -145,29 +174,33 @@ class TActiveRecordRelationContext
* Creates and return the TActiveRecordRelation handler for specific relationships.
* An instance of TActiveRecordHasOne, TActiveRecordBelongsTo, TActiveRecordHasMany,
* or TActiveRecordHasManyAssocation will be returned.
+ * @param TActiveRecordCriteria search criteria
* @return TActiveRecordRelation record relationship handler instnace.
+ * @throws TActiveRecordException if property is not defined or missing.
*/
- public function getRelationHandler()
+ public function getRelationHandler($criteria=null)
{
+ if(!$this->hasRecordRelation())
+ {
+ throw new TActiveRecordException('ar_undefined_relation_prop',
+ $property, get_class($this->_sourceRecord), self::RELATIONS_CONST);
+ }
+ if($criteria===null)
+ $criteria = new TActiveRecordCriteria;
switch($this->getRelationType())
{
case TActiveRecord::HAS_MANY:
- if(!$this->hasAssociationTable())
- {
- Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordHasMany');
- return new TActiveRecordHasMany($this);
- }
- else
- {
- Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordHasManyAssociation');
- return new TActiveRecordHasManyAssociation($this);
- }
+ Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordHasMany');
+ return new TActiveRecordHasMany($this, $criteria);
+ case TActiveRecord::MANY_TO_MANY:
+ Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordHasManyAssociation');
+ return new TActiveRecordHasManyAssociation($this, $criteria);
case TActiveRecord::HAS_ONE:
Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordHasOne');
- return new TActiveRecordHasOne($this);
+ return new TActiveRecordHasOne($this, $criteria);
case TActiveRecord::BELONGS_TO:
Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordBelongsTo');
- return new TActiveRecordBelongsTo($this);
+ return new TActiveRecordBelongsTo($this, $criteria);
default:
throw new TActiveRecordException('ar_invalid_relationship');
}