【发布时间】:2014-01-08 13:03:48
【问题描述】:
目前,我可以从 items 表中提取所有产品,但我无法匹配 sizes_id,因为它们之间的关系是在 SizesToProducts 表中设置的。
数据库表如下:
- 商品(id、product_id、数量)
- 产品(ID、名称)
- 尺寸(ID、名称)
- SizesToProducts(id、product_id、sizes_id、价格)
这是我的模型:
//--- Item Class
class Item extends \Eloquent {
protected $table = '_cart_item';
function product()
{
return $this->hasMany('App\Models\Product','id');
}
}
//--- Product Class
class Product extends \Eloquent {
protected $table = '_products';
public function items()
{
return $this->belongsTo('Item');
}
public function size()
{
return $this->hasMany('Product','product_id');
}
}
//--- Size Class
class Size extends \Eloquent {
protected $table = '_sizes';
public function product()
{
return $this->belongsTo('SizeToProduct','sizes_id');
}
}
//--- SizeToProduct Class
class SizeToProduct extends \Eloquent {
protected $table = '_sizes_to_products';
public function items()
{
return $this->belongsTo('Product','sizes_id');
}
}
【问题讨论】:
标签: php mysql laravel-4 database-relations