Welcome Guest, Not a member yet? Register   Sign In
Generic Data Retrieval Model?
#1

[eluser]Unknown[/eluser]
Hi Folks,

I am very new to CodeIgniter and I had a question about best practices. I had an idea that it would perhaps be useful to have one generic model to parse data. I thought that it might be useful to create one generic model to reuse it again and again, but I am not sure if this is a violation of "MVC" principles or not.

Here's the code in my controller:
Code:
$data['holidays'] = $this->fetch_data->arFetch('cal','full_date',array('holiday'), array('holiday.cal_id = cal.id'),'');

Here's the code in my model:
Code:
function arFetch($table, $cols, $join, $joinOn, $where) {
  $joinOn = array_combine($join, $joinOn);
  foreach($joinOn as $t => $clause) {
   $this->db->join($t, $clause);
  }
  #$this->db->where($where);
  
  $q = $this->db->get($table);

  if ($q->num_rows() > 0) {
   return $q;
   }

While I first thought this was a terrific idea, now I am not so sure. I almost feel as though I am using the model as a function, which does not seem to be the appropriate purpose.

Your thoughts would be greatly appreciated.

Thanks for the help everyone!

Kind Regards,
Tom G
#2

[eluser]alexwenzel[/eluser]
You can checkout my CRUD model (below in my signature). Maybe it fits better your needs.
#3

[eluser]solid9[/eluser]
Don't reinvent the wheel.
study MY_Model.
#4

[eluser]Unknown[/eluser]
[quote author="solid9" date="1351767180"]Don't reinvent the wheel.
study MY_Model.
[/quote]

This is what I was seeking. While I understand that it is not difficult to write SQL, I'd like something to make it easier to use active records and the like. I like the database agnosticism and the auto escaping, but having many lines of code in the model each time I want to build a query the "AR" way seems unnecessary.

Have a great day and thanks again for the response!




Theme © iAndrew 2016 - Forum software by © MyBB