【发布时间】:2014-09-19 14:31:12
【问题描述】:
我很难理解 laravel 的 hasManyThrough 概念。 我有三张桌子:
Bookings
-id (int)
-some other fields
Meta
-id (int)
-booking_id (int)
-metatype_id (int)
-some other fields
MetaType
-id (int)
-name (string)
-some other fields
我想要得到的是一个 Eloquent 模型,它允许我拥有一个带有多个 MetaType 类型的 Meta 记录的预订记录。我认为 hasManyThrough 可能已经解决了这个问题,但现在我认为这可能不是最好的方法。
在我的预订模式中,我有
public function bookingmeta() {
return $this->hasMany('bookingmeta','booking_id');
}
public function bookingmetatype() {
return $this->hasManyThrough('bookingmetatype','bookingmeta','booking_id','bookingmetatype_id');
}
但这无法生成正确的 SQL 并失败。我明白了
select `new_bookingmetatype`.*, `new_bookingmeta`.`booking_id`
from `new_bookingmetatype`
inner join `new_bookingmeta`
on `new_bookingmeta`.`bookingmetatype_id` = `new_bookingmetatype`.`id`
where `new_bookingmeta`.`booking_id` in (57103)
而我真正想要实现的是
select `new_bookingmetatype`.*, `new_bookingmeta`.`booking_id`
from `new_bookingmetatype`
inner join `new_bookingmeta`
on `new_bookingmeta`.`id` = `new_bookingmetatype`.`bookingmetatype_id`
where `new_bookingmeta`.`booking_id` in (57103)
如果有人能指出我正确的方向,我将不胜感激。谢谢。
【问题讨论】: