Class TActiveRecordBelongsTo

Description

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

Located in /Data/ActiveRecord/Relations/TActiveRecordBelongsTo.php (line 71)

TActiveRecordRelation
   |
   --TActiveRecordBelongsTo
Method Summary
void collectForeignObjects (array &$results)
void setObjectProperty (TActiveRecord $source, array $properties, mixed &$collections)
Methods
collectForeignObjects (line 78)

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

  • access: protected
void collectForeignObjects (array &$results)
  • array &$results: original results.

Redefinition of:
TActiveRecordRelation::collectForeignObjects()
getRelationForeignKeys (line 93)
  • return: foreign key field names as key and object properties as value.
  • access: public
  • since: 3.1.2
array getRelationForeignKeys ()

Redefinition of:
TActiveRecordRelation::getRelationForeignKeys()
setObjectProperty (line 104)

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

  • access: protected
void setObjectProperty (TActiveRecord $source, array $properties, mixed &$collections)
  • TActiveRecord $source: source object.
  • array $properties: foreign objects.

Redefinition of:
TActiveRecordRelation::setObjectProperty()
Sets the foreign objects to the given property on the source object.
updateAssociatedRecords (line 122)

Updates the source object first.

  • return: true if all update are success (including if no update was required), false otherwise .
  • access: public
boolean updateAssociatedRecords ()

Inherited Methods

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