Permalink
Browse files

* Added index option to AR relations so that related objects can be i…

…ndexed using the values of a specific column
  • Loading branch information...
1 parent 33613df commit 1ff28a4946b96d075cd1afa68e2b69da25cd4f35 qiang.xue committed Jun 23, 2009
Showing with 39 additions and 5 deletions.
  1. +1 −0 CHANGELOG
  2. +4 −0 docs/guide/changes.txt
  3. +6 −0 docs/guide/database.arr.txt
  4. +5 −1 framework/db/ar/CActiveFinder.php
  5. +23 −4 framework/db/ar/CActiveRecord.php
View
@@ -18,6 +18,7 @@ Version 1.0.7 to be released
- New #396: Improved error display when running yiic commands (Qiang)
- New: Rolled back the change about treating tinyint(1) in MySQL as boolean (Qiang)
- New: Added support for displaying call stack information in trace messages (Qiang)
+- New: Added 'index' option to AR relations so that related objects can be indexed by specific column value (Qiang)
Version 1.0.6 June 7, 2009
--------------------------
View
@@ -9,6 +9,10 @@ Version 1.0.7
* Added support for displaying call stack information in trace messages
- [Logging Context Information](/doc/guide/topics.logging#logging-context-information)
+ * Added `index` option to AR relations so that related objects can be indexed
+using the values of a specific column
+ - [Relational Query Options](/doc/guide/database.arr#relational-query-options)
+
Version 1.0.6
-------------
@@ -301,6 +301,12 @@ references need to be disambiguated using `aliasToken` (e.g. `??.age`).
references need to be disambiguated using `aliasToken` (e.g. `??.age`).
Note: option has been available since version 1.0.1.
+ - `index`: the name of the column whose values should be used as keys
+of the array that stores related objects. Without setting this option,
+an related object array would use zero-based integer index.
+This option can only be set for `HAS_MANY` and `MANY_MANY` relations.
+This option has been available since version 1.0.7.
+
In addition, the following options are available for certain relationships
during lazy loading:
@@ -627,7 +627,11 @@ private function populateRecord($query,$row)
$fpk=0;
if(!isset($this->_related[$pk][$child->relation->name][$fpk]))
{
- $record->addRelatedRecord($child->relation->name,$childRecord,true);
+ if($childRecord instanceof CActiveRecord && $child->relation->index!==null)
+ $index=$childRecord->{$child->relation->index};
+ else
+ $index=true;
+ $record->addRelatedRecord($child->relation->name,$childRecord,$index);
$this->_related[$pk][$child->relation->name][$fpk]=true;
}
}
@@ -726,6 +726,9 @@ public function primaryKey()
* <li>'on': the ON clause. The condition specified here will be appended
* to the joining condition using the AND operator. This option has been
* available since version 1.0.2.</li>
+ * <li>'index': the name of the column whose values should be used as keys
+ * of the array that stores related objects. This option is only available to
+ * HAS_MANY and MANY_MANY relations. This option has been available since version 1.0.7.</li>
* </ul>
*
* The following options are available for certain relations when lazy loading:
@@ -921,16 +924,23 @@ public function setAttribute($name,$value)
* This method is used internally by {@link CActiveFinder} to populate related objects.
* @param string attribute name
* @param mixed the related record
- * @param boolean whether the relation is HAS_MANY/MANY_MANY.
+ * @param mixed the index value in the related object collection.
+ * If true, it means using zero-based integer index.
+ * If false, it means a HAS_ONE or BELONGS_TO object and no index is needed.
*/
- public function addRelatedRecord($name,$record,$multiple)
+ public function addRelatedRecord($name,$record,$index)
{
- if($multiple)
+ if($index!==false)
{
if(!isset($this->_related[$name]))
$this->_related[$name]=array();
if($record instanceof CActiveRecord)
- $this->_related[$name][]=$record;
+ {
+ if($index===true)
+ $this->_related[$name][]=$record;
+ else
+ $this->_related[$name][$index]=$record;
+ }
}
else if(!isset($this->_related[$name]))
$this->_related[$name]=$record;
@@ -2060,6 +2070,12 @@ class CHasManyRelation extends CActiveRelation
* @var integer offset of the rows to be selected. It is effective only for lazy loading this related object. Defaults to -1, meaning no offset.
*/
public $offset=-1;
+ /**
+ * @var string the name of the column that should be used as the key for storing related objects.
+ * Defaults to null, meaning using zero-based integer IDs.
+ * @since 1.0.7
+ */
+ public $index;
/**
* Merges this relation with a criteria specified dynamically.
@@ -2074,6 +2090,9 @@ public function mergeWith($criteria)
if(isset($criteria['offset']) && $criteria['offset']>=0)
$this->offset=$criteria['offset'];
+
+ if(isset($criteria['index']))
+ $this->index=$criteria['index'];
}
}

0 comments on commit 1ff28a4

Please sign in to comment.