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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?