How to display a foreign key field's referenced name value instead of a dropdown select

edited July 2013 in Modules
Hi guys, here's something I hope can be answered simply, just not sure how to override the default form_fields() behavior to make this happen.

I have a "Products" table and an "Orders" table. For each order I have a "productId" field serving as a foreign key to identify the name of the product that was ordered. I have more than one product that can be ordered, and what I want is for the Edit screen of each order in Fuel to display the text of the referenced name of the product on that order instead of a dropdown that allows someone to select a different product for that order. I would like the productId field to be 'frozen' once the order is placed and to not allow anyone to change it to something else.

I've tried setting the displayonly property in the form_fields() function, but that just displays the productId number, and what I want is the referenced "name" field from the Products table to display here. What other code do I need to write to pull that name down instead of the id?

Erik

Comments

  • edited 1:26PM
    hi erik,
    i had the same problem but as i was playing around. i fixed it in my case.
    When u define your foreign_key, the options that gets displayed is the second column in your database table. for example, if your fields in db table is "id", "name", "address".. then when the foreign_key is defined in your model then all the "name" column is displayed in your select form element.
  • Hmm. I've concluded the same. But what if I want the 3rd column?
  • edited 1:26PM
    You can overwrite the your options_list method on your model. The signature is the following and is inherited from MY_Model:
    function options_list($key = NULL, $val = NULL, $where = array(), $order = TRUE)
    The first parameter is the value in the drop down and the second value is the label.
Sign In or Register to comment.