Welcome Guest, Not a member yet? Register   Sign In
Function naming - controller vs model. What is right approach?
#1

[eluser]Michal1[/eluser]
Hello guys I have a simple questions. What is the right approach in naming function in controller vs model? I mean I know that due to OOP I can have something like:

Code:
public function get_data()
{

}

both in model and controller and everything will be fine. But is this standard and recommende approach? Or should I use something else.

Let say in my model I have function

Code:
public function add_task($data)
{

  $this->db->insert('data',$data);
  return;
  
}


and then in controller


Code:
public function add_task()
{
  $data = array (
  'task_name' => $this->input->post('text'));
  
  if ($this->input->post('text'))
  {
  $this->site_model->add($data);
  redirect ('site');
  }
  else
  {
   echo "You need to write insert something ";
  
  
  }
}

is this right? Or should I name them differently? Thank you
#2

[eluser]srpurdy[/eluser]
That's really up to you and what you think will be easier to understand if someone else were to use your code. If it's for yourself. I would do it the way that works best for you.

I tend to use different function names simply because in many cases you will be doing many queries in 1 controller function, but multiple model functions. So you can't really have the same name for all of them. So I usually name it something that gives me a clue as to what data it will get from the database, and it's always in a model that's named after the controller so that each controller has it's own model.

for example if you end up with complicated functions. you need to know which data you will be getting.

Code:
function show_champ()
{
$data = $this->normal_view();
  if(!$data['league']->result())
  {
  $this->leagues_error->error_four_zero_four();
  }
  else
  {
   if($data['date'] > $data['expire'])
   {
   $this->leagues_error->expired();
   }
   else
   {
    if($data['do_count'] == '0' OR $data['do_count'] > '0' AND $data['do_count'] <= $data['new_l'] AND $data['do_count_sea'] == '0' OR $data['do_count_sea'] > '0' AND $data['do_count_sea'] <= $data['new_l_sea'])
    {
    $data['champ_info'] = $this->champ_model->get_champ_info();
    $data['get_leader'] = $this->champ_model->points_leader();
    $data['get_leader_nmc'] = $this->champ_model->points_leader_nmc();
    $data['get_leader2'] = $this->champ_model->points_leader2();
    $data['get_leader3'] = $this->champ_model->points_leader3();
    $data['team_leader'] = $this->champ_model->team_points_leader();
    $data['team_leader1'] = $this->champ_model->team_points_leader1();
    $data['team_leader2'] = $this->champ_model->team_points_leader2();
    $data['team_leader3'] = $this->champ_model->team_points_leader3();
    $data['get_logo'] = $this->series_model->get_logo();
    foreach($data['get_leader'] as $lead)
    {
    $data['pts_total'] = $lead->champ_points;
    }
    foreach($data['get_leader_nmc'] as $lead_nmc)
    {
    $data['pts_total_nmc'] = $lead_nmc->champ_points;
    }
    foreach($data['get_leader2'] as $lead2)
    {
    $data['pts_total2'] = $lead2->champ_points;
    }
    foreach($data['get_leader3'] as $lead3)
    {
    $data['pts_total3'] = $lead3->champ_points;
    }
    foreach($data['team_leader'] as $tl)
    {
    $data['team_total'] = $tl->champ_points;
    }
    foreach($data['team_leader1'] as $tl)
    {
    $data['team_total1'] = $tl->champ_points;
    }
    foreach($data['team_leader2'] as $tl2)
    {
    $data['team_total2'] = $tl2->champ_points;
    }
    foreach($data['team_leader3'] as $tl3)
    {
    $data['team_total3'] = $tl3->champ_points;
    }
    $data['sea'] = $this->season_model->edit_season();
    foreach($data['sea']->result() as $drop)
    {
    $data['dr'] = $drop->drop_races;
    }
    //$this->write_championship_standings_xml($this->uri->segment(4));
    $data['get_champfields'] = $this->leagues_model->show_champfields();
    $data['show_champ'] = $this->champ_model->get_champ($this->uri->segment(5));
    $data['show_champ1'] = $this->champ_model->get_champ1($this->uri->segment(5));
    $data['show_champ2'] = $this->champ_model->get_champ2($this->uri->segment(5));
    $data['show_champ3'] = $this->champ_model->get_champ3($this->uri->segment(5));
    $data['champ_teams'] = $this->champ_model->get_champ_team();
    $data['champ_teams1'] = $this->champ_model->get_champ_team1();
    $data['champ_teams2'] = $this->champ_model->get_champ_team2();
    $data['champ_teams3'] = $this->champ_model->get_champ_team3();
    $data['team_champ'] = $this->champ_model->get_team_champ();
    $data['team_champ1'] = $this->champ_model->get_team_champ1();
    $data['team_champ2'] = $this->champ_model->get_team_champ2();
    $data['team_champ3'] = $this->champ_model->get_team_champ3();
    $data['series'] = $this->series_model->show_series_public();
    $data['season'] = $this->season_model->show_seasons_public();
    $data['countdl'] = '0';
    foreach($data['show_champ'] as $d_lead)
    {
    $data['max_races'] = $d_lead->drop_races;
    $data['drop_r'] = $this->champ_model->champ_drop($d_lead->iracing_cust_id, $d_lead->drop_races);
     foreach($data['drop_r']->result() as $new_lead)
     {
     @$data['dr_lead'] += $new_lead->points + $new_lead->bonus_pts - $new_lead->pen_pts;;
     $data['countdl']++;
      if($data['countdl'] == $data['max_races'])
      {
      break;
      }
     }
    break;
    }
    foreach($data['champ_info'] as $c){
     $sea_t = $c->season_title;
     $ser_t = $c->series_title;
    }
    $data['heading'] = $sea_t . ' ' . $ser_t;
    $league_temp2 = $this->leagues_model->get_league_temp2();
    $league_temp_noside = $this->leagues_model->get_league_temp_noside();
    $data['page'] = $this->config->item('FAL_template_dir'). $league_temp2. '/show_champ';
    $league_temp = $this->leagues_model->get_league_temp();
    $this->_container = $this->config->item('FAL_template_dir'). $league_temp_noside;
    $this->load->vars($data);
    $this->load->view($this->_container);
    }
    else
    {
     if($data['do_count'] > $data['new_l'] OR $data['do_count_sea'] > $data['new_l_sea'])
     {
     $this->leagues_error->downgraded();
     }
    }
   }
  }
}

This code is like 2 years old. I'd do things a bit differently now, but gives you idea of how you can have many model function calls in your controller function.

Shawn




Theme © iAndrew 2016 - Forum software by © MyBB