yiisoft / yiisoft/active-record
relations cannot be linked via multiple tables
@cebe is already working on this.
Since Jun 2, 2017.
- Dominant language
- PHP
- Stars
- 119
- Forks
- 38
- Avg merge
- 1h 11m
- Merged PRs (30d)
- 1
Description
my understanding of a relational database is that there can be many kinds of relations, yet yii only supports single table relations. i find this extremely limiting.
for example, i have tags that can be assigned to an item via an ItemTag table and also via a CustomTag table. in my item, i want to define a single getTags() relation that gets all the tags assigned via both tables and sorts them on a condition. as far as i can tell yii is unable to perform such a task inherently. i would have to get both tag lists separately and sort them manually in php.
$popularityQuery = (new \yii\db\Query())
->select( ['COUNT(*) AS popularity', 'tag_id'] )
->from( 'item_tag' )
->groupBy( 'tag_id' );
$this->hasMany( Tag::className(), ['id' => 'tag_id'] )
->viaTable( ItemTag::tableName(), ['item_id' => 'id'] )
->leftJoin( ['totals' => $popularityQuery], 'totals.tag_id = tag.id' )
->orderBy( 'popularity DESC' );
$this->hasMany( Tag::className(), ['id' => 'tag_id'] )
->viaTable( CustomTag::tableName(), ['item_id' => 'id'] )
->leftJoin( ['totals' => $popularityQuery], 'totals.tag_id = tag.id' )
->orderBy( 'popularity DESC' );
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.