CodeIgniter Forums
Use db in libraries is a good way ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Use db in libraries is a good way ? (/showthread.php?tid=61275)



Use db in libraries is a good way ? - casa - 04-06-2015

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

class 
Excel extends PHPExcel
{
    private 
$db1 ;   // my db connexion

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

    public function 
test($id)
    {
        
$sql = .... ;
        
$query $this->db1->query($sql) ;
        if(
$query->num_rows() > 0)
        {
           foreach(
$query->result() as $row)
           {
              .....
           }
        }
        
$query->free_result();
    }


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

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


Thanks for your answers and explanation.


RE: Use db in libraries is a good way ? - sintakonte - 04-07-2015

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


RE: Use db in libraries is a good way ? - gadelat - 04-07-2015

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.


RE: Use db in libraries is a good way ? - mwhitney - 04-08-2015

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.