## Class TActiveRecordHasMany

Description

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.

     +------+            +--------+
     | Team | 1 <----- * | 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.

     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 static <tt>$RELATIONS</tt> property of TeamRecord defines that the property <tt>$players</tt> has many <tt>PlayerRecord</tt>s.

The players list may be fetched as follows.

     $team = TeamRecord::finder()->with_players()->findAll();

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

- since: 3.1
- version: $Id$
- author: Wei Zhuo <weizho[at]gmail[dot]com>

Located in /Data/ActiveRecord/Relations/TActiveRecordHasMany.php (line 69)

    TActiveRecordRelation
       |
       --TActiveRecordHasMany

Method Summary
void collectForeignObjects (array &$results)
array getRelationForeignKeys ()
boolean updateAssociatedRecords ()
Methods
collectForeignObjects (line 76)

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 92)

- return: foreign key field names as key and object properties as value.
- access: public
- since: 3.1.2

array getRelationForeignKeys ()

Redefinition of:

TActiveRecordRelation::getRelationForeignKeys()

updateAssociatedRecords (line 102)

Updates the associated foreign objects.

- 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, 22 Feb 2010 12:16:34 +0000 by phpDocumentor 1.3.0RC4