Implements the M-N (many to many) relationship via association table.
Consider the entity relationship between Articles and Categories via the association table <tt>Article_Category</tt>.
Where one article may have 0 or more categories and each category may have 0 or more articles. We may model Article-Category object relationship as active record as follows.
- +---------+ +------------------+ +----------+
- | Article | * -----> * | Article_Category | * <----- * | Category |
- +---------+ +------------------+ +----------+
- class ArticleRecord
- {
- const TABLE='Article';
- public $article_id;
- public $Categories=array(); //foreign object collection.
- public static $RELATIONS = array
- (
- 'Categories' => array(self::MANY_TO_MANY, 'CategoryRecord', 'Article_Category')
- );
- public static function finder($className=__CLASS__)
- {
- return parent::finder($className);
- }
- }
- class CategoryRecord
- {
- const TABLE='Category';
- public $category_id;
- public $Articles=array();
- public static $RELATIONS = array
- (
- 'Articles' => array(self::MANY_TO_MANY, 'ArticleRecord', 'Article_Category')
- );
- public static function finder($className=__CLASS__)
- {
- return parent::finder($className);
- }
- }
The static <tt>$RELATIONS</tt> property of ArticleRecord defines that the property <tt>$Categories</tt> has many <tt>CategoryRecord</tt>s. Similar, the static <tt>$RELATIONS</tt> property of CategoryRecord defines many ArticleRecords.
The articles with categories 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>Categories</tt>) fetchs the corresponding CategoryRecords using a second query (not by using a join). The <tt>with_xxx()</tt> accepts the same arguments as other finder methods of TActiveRecord.
- $articles = TeamRecord::finder()->withCategories()->findAll();
Located in /Data/ActiveRecord/Relations/TActiveRecordHasManyAssociation.php (line 85)
TActiveRecordRelation | --TActiveRecordHasManyAssociation
Get the foreign key index values from the results and make calls to the database to find the corresponding foreign objects using association table.
Fetches the foreign objects using TActiveRecord::findAllByIndex()
SQL inner join for M-N relationship via association table.
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:44 -0400 by phpDocumentor 1.3.0RC4