Packages:
default
System
System.Caching
System.Collections
System.Data
System.Data.ActiveRecord
System.Data.ActiveRecord.Relations
System.Data.ActiveRecord.Scaffold
System.Data.ActiveReecord.Scaffold.InputBuilder
System.Data.Commom.Sqlite
System.Data.Common
System.Data.Common.Mssql
System.Data.Common.Mysql
System.Data.Common.Oracle
System.Data.Common.Pgsql
System.Data.Common.Sqlite
System.Data.DataGateway
System.Data.SqlMap
System.Data.SqlMap.Configuration
System.Data.SqlMap.Statements
System.Exceptions
System.I18N
System.IO
System.Security
System.Util
System.Web
System.Web.Services
System.Web.UI
System.Web.UI.ActiveControls
System.Web.UI.WebControls
System.Web.UI.WebControls.assets
System.Xml


Classes:
Keyword

Class TActiveRecordHasOne

TActiveRecordRelation
   |
   --TActiveRecordHasOne

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. 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.

  1. +-----+ +--------+
  2. | Car | 1 <----- 1 | Engine |
  3. +-----+ +--------+
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.
  1. class CarRecord extends TActiveRecord
  2. {
  3. const TABLE='car';
  4. public $car_id; //primary key
  5. public $colour;
  6.  
  7. public $engine; //engine foreign object
  8.  
  9. public static $RELATIONS=array
  10. (
  11. 'engine' => array(self::HAS_ONE, 'EngineRecord')
  12. );
  13.  
  14. public static function finder($className=__CLASS__)
  15. {
  16. return parent::finder($className);
  17. }
  18. }
  19. class EngineRecord extends TActiveRecord
  20. {
  21. const TABLE='engine';
  22. public $engine_id;
  23. public $capacity;
  24. public $car_id; //foreign key to cars
  25.  
  26. public static function finder($className=__CLASS__)
  27. {
  28. return parent::finder($className);
  29. }
  30. }
The static <tt>$RELATIONS</tt> property of CarRecord defines that the property <tt>$engine</tt> that will reference an <tt>EngineRecord</tt> instance.

The car record with engine property list may be fetched as follows.

  1. $cars = CarRecord::finder()->with_engine()->findAll();
The method <tt>with_xxx()</tt> (where <tt>xxx</tt> is the relationship property name, in this case, <tt>engine</tt>) fetchs the corresponding EngineRecords using a second query (not by using a join). The <tt>with_xxx()</tt> accepts the same arguments as other finder methods of TActiveRecord, e.g. <tt>with_engine('capacity < ?', 3.8)</tt>.

Since: 3.1
Author: Wei Zhuo <weizho[at]gmail[dot]com>

Method Summary
protected  void
collectForeignObjects ( array &$results)
Get the foreign key index values from the results and make calls to the database to find the corresponding foreign objects.
array
protected  void
setObjectProperty ( TActiveRecord $source, array $properties, mixed &$collections)
Sets the foreign objects to the given property on the source object.
boolean
Updates the associated foreign objects.
Methods Inherited From TActiveRecordRelation
TActiveRecordRelation::fetchResultsInto(), TActiveRecordRelation::findForeignKeys(), TActiveRecordRelation::findForeignObjects(), TActiveRecordRelation::getContext(), TActiveRecordRelation::getCriteria(), TActiveRecordRelation::getIndexValues(), TActiveRecordRelation::getObjectHash(), TActiveRecordRelation::getSourceRecord(), TActiveRecordRelation::populateResult(), TActiveRecordRelation::setObjectProperty(), TActiveRecordRelation::setResultCollection(), TActiveRecordRelation::__call()

Method Details

collectForeignObjects

protected void collectForeignObjects (array &$results )

Get the foreign key index values from the results and make calls to the database to find the corresponding foreign objects.

Input
array&$resultsoriginal results.
Output
Exception

getRelationForeignKeys

public array getRelationForeignKeys ()

Output
array foreign key field names as key and object properties as value.
Exception

setObjectProperty

protected void setObjectProperty (TActiveRecord $source , array $properties , mixed &$collections )

Sets the foreign objects to the given property on the source object.

Input
TActiveRecord$sourcesource object.
array$propertiesforeign objects.
mixed&$collections
Output
Exception

updateAssociatedRecords

public boolean updateAssociatedRecords ()

Updates the associated foreign objects.

Output
boolean true if all update are success (including if no update was required), false otherwise .
Exception