• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use db in libraries is a good way ?

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 ...') ;

Excel extends PHPExcel
$db1 ;   // my db connexion

public function __construct()
parent::__construct() ;
$CI = &get_instance() ;
$this->db1 $CI->db ;

    public function 
$sql = .... ;
$query $this->db1->query($sql) ;
$query->num_rows() > 0)
$query->result() as $row)

My Controller :
PHP Code:
//_______ my controller
class Excel_controller extends CI_Controler
      public function 
$this->load->library('Excel') ;

     public function 
$id = (int) $this->security->xss_clean($id) ;
$this->excel->test($id) ;

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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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