CodeIgniter Forums

Full Version: Best Practices in Application Organization
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]a tired machine[/eluser]
The site I have been developing is about to undergo a major conceptual overhaul. I'm seeing this as an opportunity to make some improvements in how the application is organized.

What are the best practices when it comes to splitting up functions among models? Currently we have a handful of models, two of which hold the bulk of the functions (users model holds everything that has to do with users, and listings model holds everything that has to do with listings). Would be be better to break it down further? Should each model correspond with the database table it primarily deals with, or should it be organized more according to functionality?

Is a library appropriate for any controller-like code that must be used across various controllers?

El Forum

The added advantage of breaking it down, would be less memory consumption, but my advice would be that if it makes sense where everything is, and nothing seems out of place, just leave it as-is.

A lot of folks believe that a model should only handle a single table, but that's not always possible. For example, I have a statistics model, which uses just about every table in my database to generate it's statistics.

I think the definition of a library also varies from person to person. I tend to use helpers when I can, and only use a library when I require variables to be accessible to some of the functions. This means that all of your global variables are stored within the scope of the object, and you don't have to worry about naming clashes. Essentially, the library works as a name space. For me, it's the difference between having a load of documents on your desk in a big pile, and having those same documents filed in a filing cabinet.

El Forum

I generally have a model for each table which represent an item, such as a user, a message, a task (basically anything which has an auto ID), this model also joins on any foreign keys, and many to many relationships

I then use libraries to group related functionality, potentially accross many models, so I might have a messages library, which uses the messages and comments models

At the end of the day, its up to you, and CI lets you have any number of potential combinations, so just do what feels natural.
If you would just be rewriting it for the sake of doing so, or cause everyone else thinks thats how it should be done, then don't!