using model inside another model.
Hi,
I am new to fuel cms. Just started to learn fuel cms, Fuel cms is really superb makes work easier but now am facing one problem I doesn't know how to do that if anyone overcome with those issue explain me how to do that.
I am having two tables. 1st table player_info and 2nd table player_skill. player_info contains player name(varchar) and player email(varchar) field whereas player_info table contains player_id(int) and player skill( text field ).
My model name is players_model.php in this i have added form_fields() function to show form combining two tables like name as text field and email as text field and player skill in text field. Right now am getting only two fields which is in player_info table..
can anyone help me out?
Comments
public function form_fields($values = array(), $related = array()) { $fields = parent::form_fields($values, $related); // need to manually grab the value from the other table since it doesn't belong to this model if (!empty($values['id'])) { $CI =& get_instance(); $CI->load->model('player_info_model'); $player_info = $CI->player_info_model->find_one(array('player_id' => $values['id']); $skill_value = $player_info->skill; } $skill_value = (!empty($values['id'])) ? : null; $fields['skill']['comment'] = array('type' => 'textarea', 'value' => $skill_value); return $fields; }
Then you will need to alter the saving so that the skill value gets saved to the proper table
public function on_after_save($values) { $CI =& get_instance(); $CI->load->model('player_info_model'); $data = $this->normalize_save_values(); if (!empty($data['skill'])) { $save['player_id'] = $values['id']; $save['skill'] = $data['skill']; if (!$CI->player_info_model->save($save)) { $this->add_errors($CI->player_info_model->get_errors()); } } return $values; }
Thanks for your help. The way you explained is really superb thanks a lot.. I am having another doubt, on_after_save will be gets triggered after the player_models save gets executed right. To save the players_model i wanna use
public function save() {
some code to run.
}
or
it will be saved automatically?
Thanks.
Need some clarification whether it works or not for front end view using controller?
I have created the test advanced module and created first_model for view i have given the url as
'VIEW' => fuel_url(TEST_FOLDER.'/first/view/{id}')
my preview path is
test/first/{slug}
and my route is
$route['test'] = TEST_FOLDER.'/test';
$route['test/(.*)'] = TEST_FOLDER.'/test/$1';
Still now this works fine....
Exact problem occurs here
I need my preview path as
first/{slug}
and i need to route it to test controller. I have tried to apply it but it routing to fuel default controller page_router.
If this can be done. Can you explain how i need to do..
Thanks