CodeIgniter Forums

Full Version: HMVC best practice - calling models in other modules triads?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Another question related to HMVC pattern (I'm using Modular Extensions):

In my database I have a 'categories' table which I need to interact with from different modules (MVC triads), eg.

* Module A: Returns a 'search widget' where user can check which categories to be included in a search.
* Module B: A navigational tab with links to each category.
* Module C: Admin interface for creating/deleting categories

The question - should I:

implement the database interaction with the 'category' table in each module's model?
This would mean duplicate code: modules A to C would all need to have a model method like 'get_categories()' doing the pretty much the same query in the db.

Put the interaction with the categories table in it's own module - 'Categories Module' - and have the other modules call 'Categories Module' whenever categories data is needed. This seems like the right way from a maintenance point of view, but I'm not sure if this would break the HMVC philosophy of complete compartmentalizing / modularing.

c) Something else?

(I have read different introductions to HMVC on the net, but I can't really grasp what I'm supposed to do here)


El Forum

1. Never duplicate code.
2. Make common models available to the entire application via application/models.
3. If you must access a model in a different module, access it via a controller in that module.
4. Nobody really cares so long as it works.

El Forum

Thanks a lot - much appreciated !