Adding a filter to a simple module

edited February 2012 in Installation
Hi, I'm trying to add a filter to a simple module to complete a proof of concept and I'm running into some difficulties - would be much obliged if anyone could help! I've searched this forum for similar problems, but couldn't find anything to help.

I have two very simple database tables - drop_down_lists, and drop_down_list data. Details are as follows:

CREATE TABLE `drop_down_lists` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`list_name` varchar(45) DEFAULT NULL,
`field_1` varchar(45) DEFAULT NULL,
`field_2` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM

CREATE TABLE `drop_down_list_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`drop_down_list_id` int(11) DEFAULT NULL,
`field1_data` varchar(45) DEFAULT NULL,
`field2_data` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM

I then created models in /fuel/application/models:

drop_down_lists_model.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Drop_down_lists_model extends Base_module_model {

function __construct()
{
parent::__construct('drop_down_lists');
}


}

class Drop_down_list_model extends Base_module_record {

}


drop_down_list_data_model.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Drop_down_list_data_model extends Base_module_model {

public $foreign_keys = array('drop_down_list_id' => 'drop_down_lists_model');


function __construct()
{
parent::__construct('drop_down_list_data');
}

function list_items($limit = NULL, $offset = NULL, $col = 'list_name', $order = 'asc')
{
$this->db->join('drop_down_lists', 'drop_down_lists.id = drop_down_list_data.drop_down_list_id', 'left')->select('drop_down_list_data.id, drop_down_lists.list_name AS list_name, drop_down_list_data.field1_data, drop_down_list_data.field2_data', FALSE);
$data = parent::list_items($limit, $offset, $col, $order);

return $data;
}


}

finally, I added a filter in fuel/config/MY_fuel_modules.php, as follows:

$config['modules']['Drop_Down_List_Data'] = array(
'filters' => array('drop_down_lists.list_name' => array('default' => 'Title', 'label' => 'List', 'type' => 'select')),

);

This produces a drop down, but it has no values and looking at the profiler the sql has been modified with this clause:

WHERE LOWER(drop_down_lists.list_name) LIKE '%1%'

Presumably I need to pass an option list to the filter, but I can't see where or how I would do this with a simple module - any help would be much appreciated!

Comments

  • edited 5:34AM
    You will have to do a database call to get the options and insert them with something like the following:
    $options = array(); if (defined('FUEL_ADMIN')){ $CI =& get_instance(); $options = $CI->drop_down_lists_model->options_list(); } $config['modules']['Drop_Down_List_Data'] = array( 'filters' => array('drop_down_lists.list_name' => array('default' => 'Title', 'label' => 'List', 'type' => 'select', 'options' => $options)), );
    The code above checks to see if you are in the fuel admin (the constant FUEL_ADMIN is set in the Fuel_base_controller), so it doesn't incur the overhead of querying the database when you are not in the admin.

    This won't help you now, but this is going to be addressed in a later update that will allow you to pass in a model/method parameter and it will create the options based on that.
  • edited 5:34AM
    is it done?
  • edited 5:34AM
    Not yet... we are still working. Stay tuned.
Sign In or Register to comment.