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 TActiveRecordBelongsTo

TActiveRecordRelation
   |
   --TActiveRecordBelongsTo

Implements the foreign key relationship (TActiveRecord::BELONGS_TO) between the source objects and the related foreign object. Consider the entity relationship between a Team and a Player.

  1. +------+ +--------+
  2. | Team | 1 <----- * | Player |
  3. +------+ +--------+
Where one team may have 0 or more players and each player belongs to only one team. We may model Team-Player object relationship as active record as follows.
  1. class TeamRecord extends TActiveRecord
  2. {
  3. // see TActiveRecordHasMany for detailed definition.
  4. }
  5. class PlayerRecord extends TActiveRecord
  6. {
  7. const TABLE='player';
  8. public $player_id; //primary key
  9. public $team_name; //foreign key player.team_name <-> team.name
  10. public $age;
  11. public $team; //foreign object TeamRecord
  12.  
  13. public static $RELATIONS = array
  14. (
  15. 'team' => array(self::BELONGS_TO, 'TeamRecord')
  16. );
  17.  
  18. public static function finder($className=__CLASS__)
  19. {
  20. return parent::finder($className);
  21. }
  22. }
The static <tt>$RELATIONS</tt> property of PlayerRecord defines that the property <tt>$team</tt> belongs to a <tt>TeamRecord</tt>.

The team object may be fetched as follows.

  1. $players = PlayerRecord::finder()->with_team()->findAll();
The method <tt>with_xxx()</tt> (where <tt>xxx</tt> is the relationship property name, in this case, <tt>team</tt>) fetchs the corresponding TeamRecords 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_team('location = ?', 'Madrid')</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 source object first.
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 source object first.

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