yiisoft / yiisoft/active-record

relations cannot be linked via multiple tables

Open
#35 15 comments 0 reactions 1 assignee View on GitHub

@cebe is already working on this.

Since Jun 2, 2017.

type:enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.