Foreign Key

I have a selections table
with:
ID
Package_id
Selection_Text

I have the public $foreign_keys = array('packages_id' => 'Packages_model');

Packages table is:
ID
Category_ID <--foreign key
Title
Package_text

Inside the model but the select box in the CMS is populating with

But the select box for the selections table is populating with the foreign key of the Packages table
I need the selections page to populate the foreign key select box with the Title of the Packages table with the ID of the packages table the value of the option. How can I do this?

Comments

  • In your model, you can overwrite the default behavior:

    public function options_list($key = 'id', $val = 'title', $where = array(), $order = TRUE, $group = TRUE)
    {
        $data = parent::options_list($key, $val, $where, $order);
        return $data;
    }
    
  • Thank you. I'll plug it in

  • This is my Selection_model
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

    require_once(FUEL_PATH.'models/Base_module_model.php');

    class Selections_model extends Base_module_model {

    public $foreign_keys = array('packages_id' => 'Packages_model');

    function __construct()
    {
        parent::__construct('selections');
    
    }
    
    public function options_list($key = 'id', $val = 'title', $where = array('id !=0 '), $order = TRUE, $group = TRUE){
    $data = parent::options_list($key, $val, $where, $order);   
    return $data;
    

    }

    }

    class Selection_model extends Base_module_record {

    }

    Still showing category_id in the selection box.

  • Try this or just changing $val to be title;

    public function options_list($key = 'id', $val = 'title', $where = array(), $order = TRUE, $group = TRUE)
    {
        if ($val == 'category_id') {
              $val = 'title';
        }
        $data = parent::options_list($key, $val, $where, $order);
        return $data;
    }
    
  • I get the code. But Still not happening
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

    require_once(FUEL_PATH.'models/Base_module_model.php');

    class Selections_model extends Base_module_model {

    public $foreign_keys = array('packages_id' => 'Packages_model');

    function __construct()
    {
        parent::__construct('selections');
    
    }
    
    public function options_list($key = 'id', $val = 'title', $where = array('id !=0 '), $order = TRUE, $group = TRUE){
         if ($val == 'category_id') {
          $val = 'title';
    }
    $data = parent::options_list($key, $val, $where, $order);     
    return $data; 
    

    }

    }

    class Selection_model extends Base_module_record {
    }

  • If you change the title field to be the 2nd column in your table instead of the foreign key field does it work? Also, if you print out the value of $val, is it equal to 'title' before calling the parent::options_list(...)?

  • I moved the column with the ALTER command and it worked. Thank you.

Sign In or Register to comment.