Yii Framework Forum: Many to many relaton and inverseOf - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Many to many relaton and inverseOf

#1 User is offline   DeivyBrots 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 06-December 17

Posted 06 December 2017 - 04:05 AM

Hi there. Does AR supports inverseOf for many to many relations?
I have 2 tables related by third one like:

class TemplateType extends ActiveRecord
{
    public function getSites()
    {
        return $this->hasMany(Site::className(), ['id' => 'siteId'])
            ->viaTable('SiteToTemplateType', ['templateTypeId' => 'id'])->inverseOf('templateTypes');
    }
} 
class Site extends ActiveRecord
{
    public function getTemplateTypes()
    {
        return $this->hasMany(TemplateType::className(), ['id' => 'templateTypeId'])
            ->viaTable('SiteToTemplateType', ['siteId' => 'id'])->inverseOf('sites');
    }
}



Then, when i'm trying to get :

$templateType = TemplateType::find()->where(['id' => 5])->with('sites')->one();


it tells me error:

Getting unknown property: app\models\records\Site::templateTypeId


It's like trying to relate them by field in Site class, not in relational table named SiteToTemplateType.
Thanks in advance.
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,418
  • Joined: 16-February 11
  • Location:Japan

Posted 07 December 2017 - 05:31 AM

View PostDeivyBrots, on 06 December 2017 - 04:05 AM, said:

Does AR supports inverseOf for many to many relations?


Hi, welcome to the forum.
Unfortunately, the guide clearly says "No" to your question.

Quote

Note: Inverse relations cannot be defined for relations involving a junction table. That is, if a relation is defined with via() or viaTable(), you should not call inverseOf() further.

Guide > Active Record > Inverse Relations
http://www.yiiframew...verse-relations
0

#3 User is offline   DeivyBrots 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 06-December 17

Posted 07 December 2017 - 06:48 AM

Thank's for answer. Didn't saw that notice. Are you planning to make inverseOf for relations involving a junction table? If not, what's reason for not making it?
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,418
  • Joined: 16-February 11
  • Location:Japan

Posted 07 December 2017 - 07:56 AM

I'm not a developer of the framework, so the following is only a humble opinion of mine.

IMO, "inverseOf" has a very limited range of use cases.

It may be useful for "hasMany" relation where the inverse of it is just one model instance. But the inverse of "hasOne", which can be many instances, is not very easy to use.
Please read the description of the API for the detail:
http://www.yiiframew...html#inverseOf()-detail

A has_many relation via junction table can be divided to a pair of has_many and has_one relations.

For example, a TemplateType has many TemplateTypeToSites and a TemplateTypeToSite has one Site.
Since you can define "inverseOf"s for those 2 simple relations, you would be able to make use of the "inverseOf" feature using this pair of the relations.

But I'm afraid it's not very easy to use nor very useful.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users