using model inside another model.

edited October 2015 in Modules
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

  • edited 10:26PM
    If you want to combine the fields into one form, you can add a text field in your players_model::form_fields method, you can add additional fields like the following:
    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; }
  • edited 10:26PM
    Hi admin,
    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.
  • edited 10:26PM
    on_after_save get's executed automatically upon calling the save method on the model (e.g. $model->save()).
  • edited 10:26PM
    Thanks admin. Its working great.
    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
  • edited 10:26PM
    First, does the route first/{slug} work at all... meaning, does it route to your "test" controller? Second, where in the file structure does your "test" controller exist and what is the file name of the controller?
  • edited 10:26PM
    no first/{slug} didn't wrk. No it not routed to test controller it routed to default router (i.e)page_router controller. My test controller exist in test module. controller name is test
  • edited 10:26PM
    If you have a controller name the same as your advanced module, then you shouldn't need to create an additional route and test/first would map to fuel/modules/test/controllers/test.php with the "first" method.
  • edited 10:26PM
    ok.. It works great.. thanks..
Sign In or Register to comment.