Welcome Guest, Not a member yet? Register   Sign In
models / controllers / design feedback needed..
#1

[eluser]Noy Gabay[/eluser]
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!!
#2

[eluser]Ben Edmunds[/eluser]
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.
#3

[eluser]BrianDHall[/eluser]
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.
#4

[eluser]Noy Gabay[/eluser]
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..
#5

[eluser]clip[/eluser]
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.
#6

[eluser]Noy Gabay[/eluser]
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!
#7

[eluser]clip[/eluser]
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.
#8

[eluser]jedd[/eluser]
[quote author="Noy Gabay" date="1256851445"]
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.).
[/quote]

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?




Theme © iAndrew 2016 - Forum software by © MyBB