set_model

void set_model( string $model_as_string );

Sets the model that crud will use .The model always must extends grocery_CRUD_Model . grocery_CRUD_Model extends CI_Model so you don't have to be afraid to use it.

Below I explain with steps how you can use the set_model

1st STEP - go to your basic function and add your custom model

function just_an_example()
{    
    $crud = new grocery_CRUD();
 
    $crud->set_model('My_Custom_model');
    $crud->set_table('film');
    $crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname','priority');  
 
    $output = $crud->render();
 
    $this->_example_output($output); 
 
}

2nd STEP-Your custom model MUST extend grocery_CRUD_Model. So create a new file at application/models/my_custom_model.php and it will be something like this:

class My_Custom_model extends grocery_CRUD_Model  {
 
    function get_relation_n_n_unselected_array($field_info, $selected_values)
 
    {
        $selection_primary_key = $this->get_primary_key($field_info->selection_table);
 
        if($field_info->name = '....')
 
        {
            $this->db->where(....);
            .......your custom queries
        }
 
        $this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}");
 
        $results = $this->db->get($field_info->selection_table)->result();
 
 
        $results_array = array();
        foreach($results as $row)
 
        {
            if(!isset($selected_values[$row->{$field_info->primary_key_alias_to_selection_table}]))
                $results_array[$row->{$field_info->primary_key_alias_to_selection_table}] = $row->{$field_info->title_field_selection_table}; 
        }
 
 
        return $results_array;        
    }
 
}

The set_model function is good because you don't have to change the core of grocery crud. You can create whatever model you like if you just want to do more complicated queries.

For another example you can also see a full working example of using the set_model »

Latest download
grocery CRUD Newsletter
Join our newsletter to receive our last updates and news.
* indicates required