【问题标题】:laravel 5.6 Eloquent One to Many Relationship on multiple tableslaravel 5.6 Eloquent 一对多关系在多个表上
【发布时间】:2018-12-14 17:23:45
【问题描述】:

我有一些特定的数据库结构如下图

所以基本上一个零件可以有多个规格,但这些规格不存在于已知表中。规范可以在任何 rsnf_part_specificationrgr_part_specificationdcd_part_specification 表中。我需要找到一种方法来通过 part_owner_short_code FK 了解哪个表,因为它的规格表前缀为 rsnf,dcd 或 rgr

是否可以使用 Laravel 5.6 Eloquent Relations 做到这一点?

【问题讨论】:

  • 有 22 个零件所有者,我刚才提到了其中的 3 个,这意味着将有 22 个“OWNERPREFIX_part_specifications”表。我会

标签: php database model eloquent laravel-5.6


【解决方案1】:

你必须像这样修改你的表结构

owners(id, code, status)
parts(id, owner_id, category_id, name) //should add owner_id as FK 
part_specifications(id, part_id, name, description) //no need to prefix owner code

所有者模型

class Owner extend Model {
    protected $table = 'owners';

    public function parts(){
        return $this->hasMany('App\Part', 'owner_id');
    }

    public function partSpecification(){
        return $this->hasManyThrough('App\PartSpecification', 'App\Part', 'owner_id', 'part_id');
    }

}

零件模型

class Part extend Model {
    protected $table = 'parts';

    public function owner(){
        return $this->belongsTo('App\Owner', 'owner_id');
    }

    public function category(){
        return $this->belongsTo('App\Category', 'category_id'); // Define Category model
    }
}

零件规格模型

class PartSpecification extend Model {
    protected $table = 'part_specifications';

    public function part(){
        return $this->belongsTo('App\Part', 'part_id');
    }
}

编辑:

如果你想使用现有的规范结构,那么试试这个

所有者模型

class Owner extend Model {
    protected $table = 'owners';

    public function parts(){
        return $this->hasMany('App\Part', 'owner_id');
    }
}

零件模型

    class Part extend Model {
        protected $table = 'parts';

        public function owner(){
            return $this->belongsTo('App\Owner', 'owner_id');
        }

        public function category(){
            return $this->belongsTo('App\Category', 'category_id'); // Define Category model
        }

        public function rnsPartSpecification(){
            return $this->hasMany('App\RnsPartSpecification','part_id'); //define RnsPartSpecification model 
        }

        public function rgrPartSpecification(){
            return $this->hasMany('App\RgrPartSpecification','part_id'); //define RgrPartSpecification model 
        }

        public function dcdPartSpecification(){
            return $this->hasMany('App\DcdPartSpecification','part_id'); //define DcdPartSpecification model 
        }
    }

获取数据

$parts = Part::with('owner', 'RsnfPartSpecification', 'RgrPartSpecification', 'DcdPartSpecification')->get();

foreach($parts as $part){
    if($part->owner->code == 'rsnf'){
        print_r($part->rnsPartSpecification)
    }else if($part->owner->code == 'rgr'){
        print_r($part->rgrPartSpecification)
    }else{
        print_r($part->dcdPartSpecification)
    }
}

【讨论】:

  • 感谢您的回答。问题是我必须在多个表上分发零件规格。因为数据非常庞大,每个规格表都有大约 150,000 条记录,这是我分隔表格并为其添加前缀的方式):
  • 我已经用你现有的规范结构更新了答案,试试吧
  • 我很欣赏@rkj 和我所拥有的选项之一,但它也可能是昂贵的查询。请记住,我刚刚提到了 22 个部分所有者,其中 3 个表示 22 个“_part_specifications”表。我将使用此信息更新我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 2019-06-13
  • 2021-04-21
  • 2019-06-23
  • 2018-10-05
  • 2019-03-26
  • 2017-04-20
相关资源
最近更新 更多