Trying to access the keys of the foreign key field in view
  • Hi,

    So I have successfully added the foreign key feature between 2 of my custom modules the first module i.e., awards has press_release id's associated to it in the database table.

    Before adding the foreign key feature I was using the below fragment to get the awards from database into the view.

    $award_list = fuel_model('awards', array('find' => 'all', 'where' => "type='Awards'", 'order' => 'visible_order desc', 'limit' => 2));


    I tried making many edits to the above function in order to get the name column from the foreign key associated table. I tired referring to the documentation http://docs.getfuelcms.com/general/models#highlighter_805078 and made necessary edits by changing the find parameter to 'key', but nothing worked.

    Here is my awards model

    class Awards_model extends Base_module_model{

    public $required = array('title', 'type');
    public $unique_fields = array('slug');
    public $foreign_keys = array('press_release_id' => array(FUEL_FOLDER => 'pressreleases_model'));

    public function __construct(){
    parent::__construct('awards');
    }

    function list_items($limit = NULL, $offset = 0, $col = 'id', $order = 'asc', $just_count = false)
    {
    $this->db->select('id, title, type ,business_category, press_release_id, visible, visible_order', FALSE);
    $data = parent::list_items($limit, $offset, $col, $order, $just_count);
    return $data;
    }

    function form_fields($values = array(), $related = array()){

    $fields = parent::form_fields($values);

    $fields['slug'] = array('type' => 'slug', 'linked_to' => 'title');

    return $fields;
    }

    }

    Also I would like to display name of the press_release_id associated in the admin panel as well of the awards admin listing.
    So, could you please suggest a way to use fuel_model function to get the foreign key table fields.

    Thanks.
  • Is $award_list returning an array of "Award" record objects? Also do you have an Award_items_model specified?
  • Yes, $award_list returns an array when passed find => 'all' with fuel_model function.
    No, I don't have an Award_items_model specified, the above shared code is the only model code I am using.
  • You'll want to make sure that your Model has a record class associated with it as well. This link provides an example half way down. So one class that extends Base_module_model and another class that extends Base_module_record which usually resides in the same file:
    http://docs.getfuelcms.com/modules/simple

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!