CodeIgniter Forums
A question about multiple models - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: A question about multiple models (/showthread.php?tid=58723)



A question about multiple models - El Forum - 07-12-2013

[eluser]Unknown[/eluser]
Welcome everybody, I'm new here.

I have one question about models and DRY. I have few tables in my MySQL database (news, clients etc.) and for each table I have one model (news_model, clients_model, etc.) and I'm using CI ActiveRecord- my problem is that in most of them I need CRUD methods and... well they just look the same. It is good? Or should I create some sort of helpers?

This is my code, to show what I want to say:

This is my add method in page_model:
Code:
public function add($title, $add_date, $content)
            {
                  set_date($add_date);
                  $uri_segment = to_uri($title);
                  
                  $insert_data = array($this->_collumn_names[1] => $add_date,
                                       $this->_collumn_names[2] => NULL,
                                       $this->_collumn_names[3] => $uri_segment,
                                       $this->_collumn_names[4] => $title,
                                       $this->_collumn_names[5] => $content);
                  $this->db->insert($insert_data);
                  
                  return $this->db->insert_id();
            }

And this is my add method in news_model:
Code:
public function submit($add_date, $title, $content)
            {
                  set_date($add_date);
                  $cropped_content = word_limiter($content, 100);
                  
                  $insert_data = array($this->_collumn_names[1] => $add_date,
                                       $this->_collumn_names[2] => NULL,
                                       $this->_collumn_names[3] => $title,
                                       $this->_collumn_names[4] => $cropped_content,
                                       $this->_collumn_names[5] => $content);
                  $this->db->insert($this->_table_name, $insert_data);
                  
                  return $this->db->insert_id();
            }

Looks nearly the same and I'm pretty sure that this is wrong way to do it or is not?
Should I create some sort of helpers or something else?


A question about multiple models - El Forum - 07-13-2013

[eluser]Killswitch[/eluser]
Use this: https://github.com/jamierumbelow/codeigniter-base-model