Welcome Guest, Not a member yet? Register   Sign In
DRY MVC best practices to extend objects from DB
#1

[eluser]Unknown[/eluser]
Hi everyone,

I'm relatively new to CI (and web development in general) and I've come across a scenario that I'm not sure how to handle without duplicating lots of code.

What I want to do is retrieve a set of products from the database, create some new values for each product (such as in stock message, shipping date, customer price based on discount level, etc.). Some of these are a little complex, so I would rather not do them in the query itself.

Here is the relevant structure of my site:

Model: mproducts
/get_featured_products
/get_details
/search_by_text
/search_by_category
/search_by_subcategory
/get_bestsellers
/get_new_releases

Controllers:
search/new_releases
search/best_sellers
search/category
search/subcategory
search/text

Views:
detail (product details)
list (search results)
cart (haven't got here yet, but will include the same info)


If I include the code to calculate these fields in either the model or controller functions, I will have to copy and paste it several times, which I don't think is a good idea. One other post initially suggested returning the results as an object other than a stdClass object, with the necessary functions extending the class. Is that an option?

How would you go about implementing this in the most efficient way possible? Would a library or helper loaded in each function of the model be the most appropriate?

Thanks for your help!




Theme © iAndrew 2016 - Forum software by © MyBB