Welcome Guest, Not a member yet? Register   Sign In
Design Question
#1

[eluser]a21wat[/eluser]
I have a question for some of you long time developers. I am "re-writing" a fairly complex application using codeigniter. With that I have a couple of questions. Is it better to place ALL the SQL logic in a model or is it semantically acceptable to place some it in a controller and/or a view?

Currently I have been placing some SQL that is commonly used in a model. However, I have kept some sql requests for some of more unique queries within a view or a controller. This is the first time I am using a framework and while i understand how the application flows so to speak, I am a bit fuzzy on the "right" way to deal with the separation of SQL, Logic, and Presentation.

Thanks
#2

[eluser]gtech[/eluser]
I put all my database code into the model, infact each database table has its own model, if a function needs to make a call on two tables I make a decision on which model to put it in.

for example a users table might have a model with the functions create_user, remove_user, edit_user, get_user_results (makes a call to the user and results table), get_user_groups.

If the datamodel changes then I can make changes to the model without having to make major changes to the controller code
#3

[eluser]a21wat[/eluser]
So, gtech

Even if you have joins and such they go into a model?

This is where I was leaning, and you make sense in your design decision.
#4

[eluser]gtech[/eluser]
yes even a join.. I made a decision not to have a model for the lookup tables (links between many to many relationships), but I grouped functionality for each major table.

For example I may have a function that gets a list of users in a group. I made a decision to put it in the groups model as the query is group centric.

Code:
function get_group_users($groupid)
  {
      // this query might not work just gives you an idea
      $this->db->select('*');
      $this->db->from('group_user_lookups');
      $this->db->join('users','group_user_lookups.UserID = users.UserID');
      $this->db->where('GroupID',$groupid);
      $query = $this->db->get()
      return $query;
  }

sometimes it is a difficult call on which model to put it in. This is how I split my application up, which is quite large (26 datbase tables, 18 models with about 10 functions in each).. but my approach has enabled the application code to grow and remain readable
#5

[eluser]sikkle[/eluser]
And don't be afraid, they invent a big word somewhere they named "refactoring" we all do that, all days long Smile

good luck !




Theme © iAndrew 2016 - Forum software by © MyBB