⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

set_model and active record help



mztriz
  • profile picture
  • Member

Posted 04 May 2012 - 15:24 PM

Sorry I'm really new to PHP/CodeIngniter/MVC so please don't be too harsh.

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.

mztriz
  • profile picture
  • Member

Posted 04 May 2012 - 17:07 PM

Maybe this will be helpful to someone:


[img]http://i.imgur.com/xdGFK.png[/img]
[img]http://i.imgur.com/dkqqD.png[/img]

fieldju
  • profile picture
  • Member

Posted 17 May 2012 - 02:25 AM

I am also having problems creating a joined table using a custom model, so I bump this

Lickity
  • profile picture
  • Member

Posted 31 May 2012 - 00:56 AM

[quote name='mztriz' timestamp='1336145069' post='1619']
Sorry I'm really new to PHP/CodeIngniter/MVC so please don't be too harsh.

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. (??)
[/quote]

Just thought I would explain how the example works.

The Custom Model extends the Grocery CRUD model, which overrides the function 'get_relation_n_n_unselected_array' that can be found in the 'grocery_crud_model'.

When they 'set_relation_n_n', instead of the code using the default 'get_relation_n_n_unselected_array' function, it will use the new one that is in the Custom Model.

That is how you over ride only certain aspects of the model.

In your example, the join_table function you have written is indeed run, but nothing is done with the results. You are only seeing table1's data because that is the table you set in Grocery CRUD.