Use db in libraries is a good way ? |
Hello,
is using database in libraries is a good practice or not (why?), for example with PHPExcel in order to avoid an overload of memory server. My Library example : PHP Code: require_once('good path ...') ; My Controller : PHP Code: //_______ my controller Thanks for your answers and explanation.
i don't think its a good way to accomplish what you want
i would separate things here Extending PHPExcel to an own library is fine But your DB test function should be in an Excel_ModelĀ And in your Controller you call both the library and the model But at the bottom line you can manage it in the way you've described it
It doesn't matter in most cases. I would say: mix it in when your library is small, separate it to model when library is large.
What are you going to do, create a new method in your Excel class every time you want to output different data to a spreadsheet? In the end, you can setup your library however you'd like, but I wouldn't recommend mixing database code with a library that interfaces with PHPExcel, especially if memory use is your major concern.
Personally, I setup a wrapper library for PHPExcel to handle some basic initialization and allow access in a CI-friendly manner. When I need to create a spreadsheet, I get the data from my model and output it through my library. If I needed this functionality more often, I would simply be more inclined to make the library easier to use, but still wouldn't integrate the DB access into the library. If you're having memory issues when you don't integrate the DB access, you may need to look into the root cause of the memory issues, as I don't see anything in your example code that would indicate a significant savings over retrieving the data from a model. You may also want to consider caching your spreadsheet(s), if possible, if the act of creating the spreadsheet is causing problems. In the end, a spreadsheet is a View, and the library is simply there to facilitate creating that view. |
Welcome Guest, Not a member yet? Register Sign In |