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.
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.
- +------+ +--------+
- | Team | 1 <----- * | Player |
- +------+ +--------+
The static <tt>$RELATIONS</tt> property of PlayerRecord defines that the property <tt>$team</tt> belongs to a <tt>TeamRecord</tt>.
- class TeamRecord extends TActiveRecord
- {
- // see TActiveRecordHasMany for detailed definition.
- }
- class PlayerRecord extends TActiveRecord
- {
- const TABLE='player';
- public $player_id; //primary key
- public $team_name; //foreign key player.team_name <-> team.name
- public $age;
- public $team; //foreign object TeamRecord
- public static $RELATIONS = array
- (
- 'team' => array(self::BELONGS_TO, 'TeamRecord')
- );
- public static function finder($className=__CLASS__)
- {
- return parent::finder($className);
- }
- }
The team object may be fetched as follows.
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>.
- $players = PlayerRecord::finder()->with_team()->findAll();
Located in /Data/ActiveRecord/Relations/TActiveRecordBelongsTo.php (line 71)
TActiveRecordRelation | --TActiveRecordBelongsTo
Get the foreign key index values from the results and make calls to the database to find the corresponding foreign objects.
Sets the foreign objects to the given property on the source object.
Updates the source object first.
Inherited From TActiveRecordRelation
TActiveRecordRelation::__construct()
TActiveRecordRelation::collectForeignObjects()
TActiveRecordRelation::fetchResultsInto()
TActiveRecordRelation::findForeignKeys()
TActiveRecordRelation::findForeignObjects()
TActiveRecordRelation::getContext()
TActiveRecordRelation::getCriteria()
TActiveRecordRelation::getIndexValues()
TActiveRecordRelation::getObjectHash()
TActiveRecordRelation::getRelationForeignKeys()
TActiveRecordRelation::getSourceRecord()
TActiveRecordRelation::populateResult()
TActiveRecordRelation::setObjectProperty()
TActiveRecordRelation::setResultCollection()
TActiveRecordRelation::__call()
Documentation generated on Mon, 21 Apr 2008 11:29:40 -0400 by phpDocumentor 1.3.0RC4