Search code examples
fuelphp

FuelPHP how to change vehicle_id to vehicle_name from the vehicles table in index view


I am trying to retrieve a column from another related table, and display that instead of the ID number thats associated in the index view pages.

Tables:

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

When viewing fillups/index you see a list of Fillups, which have the id # associated with each user and vehicle.. i want it to say the username, and make of the vehicle instead (this are columns in the tables). I got this similarly to work in create/edit but that was just showing ALL vehicles for a specific user..

I can't find in the documentation how to do this, i know it deals specifically with the ORM. not sure though..

Screen capture of the index view page

Currently Vehicle ID pulls from the Fillups.vehicle_id column, it needs to be changed to pull from the Vehicles.vehicle_name column..

enter image description here


Solution

  • I answered my own question... by associating a has_many and belongs_to in the models i was able to call the vehicle_name in the view.

    in my fillup model i added:

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

    in my vehicle model i added:

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

    in my view i then could call the vehicle name in the foreach loop like so:

    echo $fillup->vehicle->vehicle_name;
    

    I did the same thing for username in place of user_id