【发布时间】:2014-09-11 12:16:10
【问题描述】:
我在为我的一个项目创建架构/模型时遇到问题,希望在这里获得一些帮助。
我目前有 3 个表:附件、产品和数据透视表 product_accessory
<?php
Schema::create('accessories', function(Blueprint $table)
{
$table->increments('id');
}
Schema::create('products', function(Blueprint $table)
{
$table->increments('id');
}
Schema::create('product_accessory', function(Blueprint $table)
{
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->integer('accessory_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');
$table->foreign('accessory_id')->references('id')->on('accessories');
}
现在的问题是我需要添加另一个最终取决于数据透视表关系的产品类型“适配器”,也就是说,适配器需要与产品和附件相关...
更新 这是我当前的 product_accessory_adaptor 表的样子
Schema::create('product_accessory_adaptor', function(Blueprint $table)
{
$table->increments('id');
$table->integer('product_accessory_id')->unsigned();
$table->foreign('product_accessory_id')->references('id')->on('product_accessory');
}
这样,我可以拥有许多与产品和配件相关的适配器。我的问题是如何用 eloquent 建模这种关系?
这是我现在拥有的:
自定义枢轴模型:
class ProductAccessory extends Pivot {
protected $table = 'product_accessory';
public function product()
{
return $this->belongsTo('Product');
}
public function accessory()
{
return $this->belongsTo('Accessory');
}
public function adaptors() {
return $this->hasMany('Adaptor', 'product_accessory_id');
}
}
产品及配件型号
class Accessory extends Eloquent {
public function products()
{
return $this->belongsToMany('Product', 'product_accessory', 'accessory_id', 'product_id')->withPivot();
}
public function newPivot(Eloquent $parent, array $attributes, $table, $exists)
{
if ($parent instanceof Product) {
return new ProductAccessory($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
public function adaptors()
{
return $this->hasManyThrough('Adaptor', 'ProductAccessory', 'accessory_id', 'product_accessory_id');
}
}
class Product extends Eloquent {
public function accessories()
{
return $this->belongsToMany('Accessory', 'product_accessory', 'product_id', 'accessory_id')->withPivot();
}
public function newPivot(Eloquent $parent, array $attributes, $table, $exists)
{
if ($parent instanceof Accessory) {
return new ProductAccessory($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
public function adaptors()
{
return $this->hasManyThrough('Adaptor', 'ProductAccessory', 'product_id', 'product_accessory_id');
}
}
适配器型号:
class Adaptor extends Eloquent {
protected $table = 'product_accessory_adaptor';
public function productAccessory() {
return $this->belongsTo('ProductAccessory');
}
}
更新 现在模式和模型已设置。但是,使用 hasManyThrough 关系存在一些问题。此外,在这种情况下,有什么方法可以为枢轴关系(即适配器)进行预加载?
注意
当我在 Product 或 Accessory 模型上调用 adapters() 时发生的错误是
Argument 1 passed to Illuminate\Database\Eloquent\Relations\Pivot::__construct() must be an instance of Illuminate\Database\Eloquent\Model, none given, called in /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 872 and defined
【问题讨论】:
-
你需要这个:1 自定义枢轴模型
ProductAccessory,2 2belongsTo此模型上的关系(Product和 @ 987654331@) +hasMany与Adaptor的关系,3 适配器模型与belongsTo与该枢轴模型的关系,4hasManyThrough与Adaptor的关系开启Product和Accessory。它可能看起来很复杂,但请尝试一下。 -
嘿@deczo 感谢您的回复。我试图弄清楚如何编写自定义枢轴模型,但似乎无法找到关于它的示例。 laravel.com/docs/eloquent#working-with-pivot-tables 此处的文档并未真正提供有关如何执行此操作的示例。你介意给我一些指示吗?