duankangpazhuo0347 2015-04-15 20:26
浏览 27
已采纳

多关联的多态关系

I'm trying to use an intermediary table to signify a sponsorship relationship between two different user tables (Employee & Non-Employee) with the following requirements:

  • A sponsor can be either an Employee or Non-Employee.
  • An Employee/Non-Employee can belong to only one sponsor.
  • An Employee can have many sponsors.

I noticed that Polymorphic Relationships for Laravel only support single associations. Typically they use the intermediary table in the relationship to prevent having to create multiple tables with the same signature. In my case, I need to polymorph both sides of the relationship since at any point I could have the sponsor/sponsored person belong to either table. I'm not sure if I'm going about this right, sort of stumped at the moment.

Here's what I currently have:

Employees
id
sponsor_id

NonEmployees
id
sponsor_id

Sponsors
id
sponsorable_id
sponsorable_type

Next, I setup the following models:

Models/Employee.php

public function sponsors() {
    return $this->morphMany('Sponsor', 'sponsorable');
}

Models/NonEmployee.php

public function sponsors() {
    return $this->morphMany('Sponsor', 'sponsorable');
}

Models/Sponsor.php

public function sponsorable() {
    return $this->morphTo();
}

With this setup, I was able to perform general lookup queries against the Sponsors table and then reverse engineer them to retrieve the name of the sponsor.

Sponsor::with('sponsorable')->get();
Sponsor::find(1)->sponsorable;
  • 写回答

1条回答 默认 最新

  • dongzanxun2790 2015-04-16 15:05
    关注

    I came up with the following idea to utilize the existing Polymorphic Relationship to handle multiple associations.

    First, I changed the schema to this:

    Employees
    id
    
    NonEmployees
    id
    
    Sponsors
    id
    sponsored_id
    sponsored_type
    sponsorable_id
    sponsorable_type
    

    So, I removed the sponsor_id field from each of the account type tables and added a second polymorphic relationship to the Sponsors table.

    I updated the models as follows:

    Models/Employee.php & Models/NonEmployee.php

    public function sponsorable()
    {
        return $this->morphOne('Sponsor', 'sponsorable');
    }
    
    public function sponsors()
    {
        return $this->morphMany('Sponsor', 'sponsor');
    }
    

    Models/Sponsor.php

    public function sponsor()
    {
        return $this->morphTo();
    }
    
    public function sponsorable()
    {
        return $this->morphTo();
    }
    

    Now, because Laravel doesn't support a morphManyThrough() relationship type, you'll notice I changed some of the names of the functions so that it would read a little cleaner when using the relationships since I have to go from one table through an intermediary table and then to a 3rd table to get the information I want.

    With this structure, I can do the following:

    $employee = Employee::find(2)->sponsorable->sponsor; // Gets employee's sponsored party
    $sponsors = $employee->sponsors; // Gets individual that the employee is sponsoring.
    foreach ($sponsors as $sponsor)
        echo $sponsor->sponsorable->first_name;
    $employee->sponsors()->save(new Sponsor()); // New sponsor
    $non_employee->sponsors()->save(new Sponsor()); // New sponsor
    

    I can also perform a reverse lookup:

    Sponsor::find(1)->sponsor->first_name; // Sponsoring party
    Sponsor::find(1)->sponsorable->first_name; // Party being sponsored
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)