Implements TActiveRecord::HAS_MANY relationship between the source object having zero or more foreign objects. 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 TeamRecord defines that the property <tt>$players</tt> has many <tt>PlayerRecord</tt>s.
- class TeamRecord extends TActiveRecord
- {
- const TABLE='team';
- public $name; //primary key
- public $location;
- public $players=array(); //list of players
- public static $RELATIONS=array
- (
- 'players' => array(self::HAS_MANY, 'PlayerRecord')
- );
- public static function finder($className=__CLASS__)
- {
- return parent::finder($className);
- }
- }
- class PlayerRecord extends TActiveRecord
- {
- // see TActiveRecordBelongsTo for detailed definition
- }
The players list may be fetched as follows.
The method <tt>with_xxx()</tt> (where <tt>xxx</tt> is the relationship property name, in this case, <tt>players</tt>) fetchs the corresponding PlayerRecords 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_players('age < ?', 35)</tt>.
- $team = TeamRecord::finder()->with_players()->findAll();
Located in /Data/ActiveRecord/Relations/TActiveRecordHasMany.php (line 69)
TActiveRecordRelation | --TActiveRecordHasMany
Get the foreign key index values from the results and make calls to the database to find the corresponding foreign objects.
Updates the associated foreign objects.
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:42 -0400 by phpDocumentor 1.3.0RC4