FuelPHP如何在索引视图中从vehicles表中将vehicle_id更改为vehicle_name

时间:2012-06-14 06:57:12

标签: fuelphp

我正在尝试从另一个相关表中检索一个列,并显示该列而不是索引视图页中关联的ID号。

表格

users: id, username, email
fillups: user_id, vehicle_id, mileage, fillup_date, cost, gallons
vehicles: make, model, color, vehicle_id

当查看填充/索引时,您会看到一个Fillups列表,其ID与每个用户和车辆相关联..我希望它说出用户名和车辆的制造(这是表格中的列) 。我在创建/编辑方面的工作类似,但这只是为特定用户显示所有车辆..

我在文档中找不到如何执行此操作,我知道它专门处理ORM。虽然不确定..

索引视图页面的屏幕截图

目前,车辆ID来自Fillups.vehicle_id列,需要将其更改为从Vehicles.vehicle_name列中提取..

enter image description here

1 个答案:

答案 0 :(得分:1)

我回答了我自己的问题...通过在模型中关联has_manybelongs_to,我可以在视图中调用vehicle_name

在我的填充模型中,我补充道:

protected static $_belongs_to = array('vehicle');

在我的车辆模型中我添加了:

protected static $_has_many = array('fillups');

在我看来,我可以在foreach循环中调用车辆名称,如下所示:

echo $fillup->vehicle->vehicle_name;

我为用户名代替user_id做了同样的事情