Where to place functions that work with database |
Hi guys,
I'm sure this is a very remedial question, but I'm not quite sure what is best-practice here. I have one model for each table in my database, and I have a controller that does a lot of calculations involving data from many tables. I'd like to create a bunch of functions to make my code cleaner (and hopefully take some of the code out of the controller). I thought to use helper functions, but I wasn't sure how to access database objects in the helper functions; since my "use" statements are in the controller. Thanks, Phil
Create a class or two that encapsulates your business rules. Put them in the Library folder. Then you can use them in your controller and keep your controllers slim.
Simpler is always better
@pcasalegno,
....or you could make your models do a lot of the lifting (you could use database views and private methods/functions in models that do a lot of the calculations) and return your data in a format where all you have to do is display it.
(05-07-2019, 07:01 PM)php_rocs Wrote: @pcasalegno, This is a great idea, and probably more in-keeping with the MVC concept. Thanks!
I like both ideas, and probably both are applicable depending on your code. I typically put things into my Model if it is primarily dealing with that models table and (moderately) its relations, and reserve make a separate class (Library) for something that is going to "run" my existing models (similar to Controllers but not as a integral part of the MVC user interaction).
|
Welcome Guest, Not a member yet? Register Sign In |