我试图从另一个相关表中检索一个列,并显示它而不是索引视图页面中关联的ID号。
表:
users: id, username, email
fillups: user_id, vehicle_id, mileage, fillup_date, cost, gallons
vehicles: make, model, color, vehicle_id当查看fillups/index时,您会看到一个Fillups列表,其中的id #与每个用户和车辆相关联。我希望它说的是用户名,而不是车辆(这是表中的列)。我在创建/编辑中得到了类似的结果,但这只是显示了特定用户的所有车辆。
我在文档中找不到如何做到这一点,我知道它专门处理ORM。但不确定..。
索引视图页的屏幕截图
当前,车辆ID从Fillups.vehicle_id列中提取,需要更改为从Vehicles.vehicle_name列中提取。

发布于 2012-06-14 17:03:13
我回答了我自己的问题。通过将模型中的has_many和belongs_to关联起来,我能够在视图中调用vehicle_name。
在我的填充模型中,我添加了:
protected static $_belongs_to = array('vehicle');在我的汽车模型中,我补充道:
protected static $_has_many = array('fillups');在我看来,然后我可以在foreach循环中调用车辆名,如下所示:
echo $fillup->vehicle->vehicle_name;我对用户名做了同样的事情,而不是user_id。
https://stackoverflow.com/questions/11028144
复制相似问题