Part of the EllisLab Network
   
 
models / controllers / design feedback needed..
Posted: 29 October 2009 12:24 PM   [ Ignore ]  
Summer Student
Total Posts:  11
Joined  08-24-2009

Hi y’all, I’ve got this situation with my models, and I’m not sure what’s the best practice for this matter so I thought I’d discuss it here.

I am currently structuring a website that has many privilege groups, and a bunch of controllers.
It has a forum, polls, personal zone, group zone, news etc.

In my main page, I need to preform a whole bunch of actions that result from several tables in my DB (mainly regarding it’s privileges, like what forums is the user allowed to access etc.).

now, I was wondering - what is the best practice for that - should I actually load all the modules (around 6) to preform some simple actions or create a new model that will only preform these simple queries (but may repeat some of the functions in the original modules)?

I know that writing the same function twice is a big no no as far as object oriented programming, but loading 6 modules for some simple retrieve functions seems like too much of an unneeded overhead..

Any advice would be greeted!!

Profile
 
 
Posted: 29 October 2009 01:04 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  884
Joined  07-26-2009

There is some debate regarding this in the MVC world but personally I like to create models for resources instead of just by table.

For example, I might have an auth model that works with users, groups, and permissions tables while I might also have a user model that only works with the users table.

 Signature 

BenEdmunds.com

Contact Me:                                    My Code: 

  ben.edmunds@gmail.com            Github
  @benedmunds

Profile
 
 
Posted: 29 October 2009 01:17 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  804
Joined  06-10-2009

I use ORM (DMZ) so I have to have at least one model for every table, but I like to pick one of the models to handle some extra inter-model communication duties. For instance given Users, User_Groups, and Permissions, I’d give functions to Users to handle some user-based features like returning all permissions of a user, checking to see if a given permissions is granted to a user, etc.

All in all, I wouldn’t really worry about the overhead unless you have some seriously heavy-weight functions - in which case it isn’t loading them that is your problem.

Likely even a dozen models of the same size would represent only a few kb of memory and an extremely small amount of processing labor, so I’d worry more about ease of development, maintenance, and orderliness of the design rather than a few extra load statements.

 Signature 

CreativeHalls Web Design and Printing
A few of my projects:
OurGulfCoast Property Management and Vacation Rental (ASP/.NET)
BukuBux - Money Saving Coupons and Gift Certificates (CodeIgniter, LAMP/MySQL)
Rentals800.com - Find a place to rent (CodeIgniter, LAMP/MySQL)
bdh (dot) hall (at) gmail (dotcom)

Profile
 
 
Posted: 29 October 2009 02:42 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  11
Joined  08-24-2009

First, thank you both for taking the time to read and reply.

Ben - I do not have a model-per-table structure, but more of a model-per-module (the forums have only one model, though use 3 different tables). Nevertheless, there is some overlap between users-to-forums (permissions), users and forums. I just wonder weather to suppress this overlap into one of the models and load it each time I use it in any other location or just address the same table (and possibly the same action) within two models. I must add - this will probably only matter in the website’s main page - since it is the only place I have to filter several modules with several levels of permissions for each user.

Brian - I think the same when it comes down to using one model for module, instead of 3 (at best). I think it’s messy.
But, since I have never developed a fully working website from base up using CI and MVC, I am quite unsure of what would actually be messier - loading 6 different modules or just re-creating the functions. Each of these seems to have its upsides but I am still having some difficulty in choosing the best practice. I would LOVE reading what you would do in this situation..

Again, thank you very much for replying. I am struggling with this for some time now, but right now I’m at the point in which I have to decide and start it up..

Profile
 
 
Posted: 29 October 2009 05:36 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  75
Joined  01-28-2009

You could always do a common or base model and extend your other models off of that model so they will be available in each model that you have loaded.

 Signature 

flaming-crud: Model generator for codeigniter
clip-gallery: Image Gallery helper library for codeigniter

Profile
 
 
Posted: 30 October 2009 07:07 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  11
Joined  08-24-2009

I guess you are right, but then I’ll have a bunch of functions I don’t need in all the other models (I’m assuming you’re talking about having a main page model and have the all the other models extend it..).

I don’t know if it’s a ‘good MVC’ practice, but I think I like your idea best at the moment.. Is it ‘logically’ best practice?

thank you!

Profile
 
 
Posted: 31 October 2009 10:36 PM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  75
Joined  01-28-2009

Sometimes best practice isn’t always what’s best.  Just do whatever makes the most since for your project.  If you have a model that doesn’t need any of the functions in your base then just extend the ci Model instead of your Main_model.

 Signature 

flaming-crud: Model generator for codeigniter
clip-gallery: Image Gallery helper library for codeigniter

Profile
 
 
Posted: 01 November 2009 05:18 AM   [ Ignore ]   [ # 7 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2126
Joined  06-04-2008
Noy Gabay - 29 October 2009 04:24 PM

In my main page, I need to preform a whole bunch of actions that result from several tables in my DB (mainly regarding it’s privileges, like what forums is the user allowed to access etc.).

Perhaps you could be a bit clearer on exactly the kinds of things that you are wanting to do here, and what database dependencies they have, and where / how often you need to access the resultant data.

For instance, for privileges, typically these are loaded at login and stored in the session - consequently no database activity (or indeed enquiry more complex than a session-data check) is required while the session remains active.  Approaching your actual problem / requirements from a fundamental level may be more satisfying than approaching your problem with the stated but possibly arbitrarily self-imposed limitation of needing to interact with 6 different models.

Also you appear to be using the word module a fair bit, sometimes as though its a synonym for model.  Are you using a genuine module approach here - and if so, what one?

Profile