CodeIgniter Forums
Question about reusing a model function or writing a new onw - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Question about reusing a model function or writing a new onw (/showthread.php?tid=55214)



Question about reusing a model function or writing a new onw - El Forum - 10-15-2012

[eluser]Unknown[/eluser]
I am completely new to CI so I'm still working my way through figuring our best practices.

I am building an app and I would like to load one of a couple different views based on the type of data being returned from the model. I have a table (toycat) and within that, a field named 'cattype'. I currently retrieve the data and tie it to
Code:
$data['cattype']
.

I want to load a different view file based on whether or not 'cattype' is 0,1,2 etc.

Should I write another function just to return the cattype and call it from the controller or should I just requery the model again? Not sure the best practice here. Code below:

Controller
Code:
function index($parent = null)
{
  
  if ($parent == false)
  {
   $parent = 1;
  }

  //test
  $this->load->model('category_model');
  $data['cats'] = $this->category_model->getAllChildren($parent);

  
  //seo data
  $data['title'] = 'category page title here';
  $data['description'] = 'Category description here...'; //passed to meta description tag
  //end seo data

  //send the user to the item view if the category they requested is an item list
  if (//this is where the cattype code would go currently)
  {
   $data['content'] = 'category_item_view'; //this is the name of the view
  } else {
   $data['content'] = 'category_view'; //this is the name of the view
  }
  
  //load the view
  $this->load->view('includes/template', $data);  


}


And the model
Code:
//gets all child catagories
function getAllChildren($parent_id)
{
  //get categories that are children of parent ID
  $this->db->where('parent_id',$parent_id);
  $query = $this->db->get('toycat');
  if ($query->num_rows() > 0)
  {
   return $query->result();
  }else{
   return FALSE;
  }  
}


Thanks in advance!


Question about reusing a model function or writing a new onw - El Forum - 10-15-2012

[eluser]Mr. Pickle[/eluser]
Is there a reason why you don't store the view name in the toycat table (as it seems to be depending on this)?

Furthermore: will this action take place for (almost) every controller?
If so, you can best make a Public_Controller.php in the libraries folder, let this extend codeigniter's controller and extend your Public_Controller() in every controller that needs this functionality.
You can then for example do a switch() on the $data['cattype'] in the Public_Controller() and determine the view (instead of endless elseif's of the lists is/gets longer) This also keeps it central instead of making this switch in every controller. All of this of course only usefull if you app uses more controllers :p)



Question about reusing a model function or writing a new onw - El Forum - 10-15-2012

[eluser]Unknown[/eluser]
Thanks for your reply!

No, this won't be used unless it's this particular controller.

Also, I am using an existing database and the information would have to be populated.

There will also only be 3 different possible views to load.

Thank you!