Class \Prado\Data\ActiveRecord\Relations\TActiveRecordHasManyAssociation
Consider the entity relationship between Articles and Categories via the association table Article_Category.
+---------+ +------------------+ +----------+
| Article | * -----> * | Article_Category | * <----- * | Category |
+---------+ +------------------+ +----------+
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.
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 $RELATIONS property of ArticleRecord defines that the property $Categories has many CategoryRecords. Similar, the static $RELATIONS property of CategoryRecord defines many ArticleRecords.
The articles with categories list may be fetched as follows.
$articles = TeamRecord::finder()->withCategories()->findAll();
The method with_xxx() (where xxx is the relationship property name, in this case, Categories) fetchs the corresponding CategoryRecords using a second query (not by using a join). The with_xxx() accepts the same arguments as other finder methods of TActiveRecord.
Class hierarchy
- \Prado\Data\ActiveRecord\Relations\TActiveRecordHasManyAssociation
- \Prado\Data\ActiveRecord\Relations\TActiveRecordRelation
Since: 3.1
public
|
createCommand(TSqlCriteria $criteria, array<string|int, mixed> $foreignKeys, array<string|int, mixed> $indexValues, array<string|int, mixed> $sourceKeys) : mixed
|
public
|
|
public
|
|
protected
|
collectForeignObjects(array<string|int, mixed> &$results) : mixed
Get the foreign key index values from the results and make calls to the
database to find the corresponding foreign objects using association table.
|
protected
|
createFkObject(string $type, array<string|int, mixed> $row, array<string|int, mixed> $foreignKeys) : TActiveRecord
|
protected
|
fetchForeignObjects(mixed &$results, array<string|int, mixed> $foreignKeys, array<string|int, mixed> $indexValues, array<string|int, mixed> $sourceKeys) : mixed
Fetches the foreign objects using TActiveRecord::findAllByIndex()
|
protected
|
getAssociationJoin(array<string|int, mixed> $foreignKeys, array<string|int, mixed> $indexValues, array<string|int, mixed> $sourceKeys) : string
SQL inner join for M-N relationship via association table.
|
protected
|
|
protected
|
|
protected
|
|
protected
|
|
protected
|
|
protected
|
|
protected
|
|
private
|
|
private
|
|
private
|
|
private
|
|
private
|