• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where to place functions that work with database

#1
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
Reply

#2
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
Reply

#3
@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.
Reply

#4
(05-07-2019, 06:05 PM)donpwinston Wrote: 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.

This was exactly what I was looking for.  Thanks!
Reply

#5
(05-07-2019, 08:01 PM)php_rocs Wrote: @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.

This is a great idea, and probably more in-keeping with the MVC concept.  Thanks!
Reply

#6
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).
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.