In the set_model documentation located here: http://www.grocerycr...tions/set_model
They create a new model called [color=#808080]My_Custom_model[/color] and in that model they created a function, [color=#808080]get_relation_n_n_unselected_array[/color]. However in their "basic" code it does not appear that they call their new model at all, they instead call [color=#808080]set_relation_n_n[/color]. They don't actually call their model and the custom model needs to extend CI_Model not grocery_CRUD_Model. (??)
I have two tables in a database like this:
[quote][color=#000000][font=Arial,][tblFitnessClasses] id uid title description location[/font][/color]
[color=#000000][font=Arial,][tblFitnessClassDateTimes] owner_uid startEventDate endEventDate startTime endTime days recurrence finalDate[/font][/color][/quote]
Essentially I want to join them together like this:
[quote][font=Consolas, Menlo, Monaco,](id - hidden) ([/font][font=Consolas, Menlo, Monaco,]uid [/font][font=Consolas, Menlo, Monaco,]-[/font][font=Consolas, Menlo, Monaco,] hidden[/font][font=Consolas, Menlo, Monaco,])[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]|[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]Title[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]|[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]Description[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]|[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]Location[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]|[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]Start[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]Date[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]|[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]End[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]Date[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]|[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]Start[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]Time[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]|[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]End[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]Time[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]|[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]Days[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]|[/font][font=Consolas, Menlo, Monaco,] [/font][font=Consolas, Menlo, Monaco,]Reccurence[/font]
[color=#2B91AF]Swim[/color] [color=#2B91AF]Lesson[/color] [color=#2B91AF]Level[/color] [color=#800000]1[/color] [color=#2B91AF]Gym[/color] [color=#800000]05[/color]/[color=#800000]04[/color]/[color=#800000]2012[/color] NULL [color=#800000]12[/color]:[color=#800000]30[/color] [color=#800000]1[/color]:[color=#800000]30[/color] [color=#2B91AF]Mon[/color],[color=#2B91AF]Wed[/color],[color=#2B91AF]Fri[/color] [color=#800000]2[/color][/quote]
Since this type of relationship is not yet supported in GroceryCrud I decided to try to write a custom model to take care of it.
I created a model named [color=#808080]schedule_model.php [/color]in my models folder:
<?php
class schedule_model extends CI_Model
{
public function join_table($table1, $table2)
{
$this->output->enable_profiler(TRUE);//Turns on CI debugging
$this->db->select($table1.".*,".$table2.".*");
$this->db->from($table1);
$this->db->join($table2, $table2.".owner_uid". "=" .$table1.".uid", 'left'); // Join classes and class date times by UID
$results = $this->db->get()->result();
return $results;
}
}
?>
In my controller I have a file named [color=#808080]main.php[/color] where I added this:
...
function fitnessSchedule()
{
$this->config->set_item('url_suffix', '');
$crud = new grocery_CRUD();
$crud->set_table('tblFitnessClasses');
$this->load->model('schedule_model');
$this->schedule_model->join_table('tblFitnessClasses','tblFitnessClassDateTimes');
$crud->columns('title','description','location','startEventDate','endEventDate','startTime', 'endTime', 'days', 'recurrance');
$crud->display_as('title','Event')
->display_as('description','Description')
->display_as('location','Location')
->display_as('startEventDate','Start Date')
->display_as('endEventDate','End Date')
->display_as('startTime','Start Time')
->display_as('endTime','End Time');
$crud->required_fields('title','location');
$crud->set_subject('Event');
$output = $crud->render();
$this->_example_output($output);
}
...
[b][color=#000000][font=Arial,]When I run this code I get a table with all of the required fields but the fields from table2 (tblFitnessCassDateTimes) are missing all information. The fields do not populate with its data and when I click edit it only edits table1. (??)[/font][/color][/b]
I really appreciate this library. Thanks everyone.