[SOLVED] Read and Write Database Connection

#1
[eluser]pakistanvoices[/eluser]
Hi guys
I am facing a problem,if some one help, it would be a great pleasureful to me.
I need how to make read and write connection in codeigniter. I have site completed but now on we move to dual ( separate ) connection for both read and write respectively.
Reply here or just ping me on

gtalkConfused[email protected]
msnConfused[email protected]
www.pakistanvoices.com
Thanks and good luck

#2
[eluser]majidmx[/eluser]
hi there,

You should specify two sets of DB configs :
Code:
$db['db_write']['hostname'] = "localhost";
$db['db_write']['username'] = "user";
$db['db_write']['password'] = "pass";
...

$db['db_read']['hostname'] = "localhost";
$db['db_read']['username'] = "user";
$db['db_read']['password'] = "pass";
...

and then in your model's code you have load them separately :

Code:
$this->DBr = $this->load->database('db_read', TRUE);
$this->DBw = $this->load->database('db_write', TRUE);

and then use it like this :


Code:
$db_result = $this->DBr->query('SELECT * FROM myTable');


Hope it helps,

MajiD

#3
[eluser]pakistanvoices[/eluser]
HI,
Thanks buddy but it is very difficult to change all the code accordingly. I need to modify database class for dual connection. It will act both mode by seeing the query pattern e.g. select or update etc.
Let me know is there any modification on your site.
good luck

gtalkConfused[email protected]
msnConfused[email protected]
http://www.pakistanvoices.com
Thanks and good luck

#4
[eluser]majidmx[/eluser]
Hi,

Actually that is exactly what I was thinking for when we faced the same problem in our application. I wanted to modify the DB class itself and there I could make the decision of using WRITE DB or READ DB.
But in our case we have some sections of the website in need of real-time checking with database records, and as we don't have the circular replication [for the same reason], I need some sections to do the read and write both from the same DB.

All these could be added as exceptions to the CI's DB class, but actually it is a trade off. If you hack the core it needs more attention on maintenance and upgrading the core, but it is less hassle of updating the code.
If you take the time to update the code it is easier to maintain but more work to do.
If you don't have so many exceptions in your code, then go for hacking the core, if you hacked it let us know how it goes and put some tips here for future use.

Thanks,
MajiD

#5
[eluser]pakistanvoices[/eluser]
hi,
I have did the same but there is one more problem raised.
When I try to open Profiler. It is not showing the queries executed.. I have to optimize the queries. So let me know how to resolve it ?
Thanks

#6
[eluser]majidmx[/eluser]
I usually don't use the profiler class and use my own logs instead.
I Have no idea what could be the reason for this.
Does anybody have any idea ?

#7
[eluser]pakistanvoices[/eluser]
hi,
Buddy I have no more time to spent on it. Please share me your log class so that I can see the queries time and optimize queries.
Thanks
Javaid
http://www.pakistanvoices.com

#8
[eluser]majidmx[/eluser]
Hi my friend,

I don't log the queries' execution time in my log class, I just use it for debug purposes and also for getting more information on my application's details.
But if it helps here it is :

log_model.php :
Code:
<?php
class Log_model extends Model{
    function Log_model(){
        parent::Model();
        
        $this->dbr = $this->load->database('db_read', TRUE);
        $this->dbw = $this->load->database('db_write', TRUE);        
        
        $this->load->helper('date');
    }
    
    function insertLog($message_type = 'Error' , $message = '' ){
            
        $extraInfo        =    '';
        $extraInfo        .=    "\r\n\n\t\t -- REQUEST  --"."\r\n\n";
        $extraInfo        .=    var_export($_REQUEST ,true);
        
        $extraInfo        .=    "\r\n\n\t\t -- COOKIE  --"."\r\n\n";
        $extraInfo        .=    var_export($_COOKIE , true);
        
        $extraInfo        .=    "\r\n\n\t\t -- ENV  --"."\r\n\n";
        $extraInfo        .=    var_export($_ENV ,true);
        
        $extraInfo        .=    "\r\n\n\t\t -- SERVER  --"."\r\n\n";
        $extraInfo        .=    var_export($_SERVER ,true);
        
        if(!isset($_SERVER['HTTP_REFERER'])){
            $ref    =    'N/A';
        }else{
            $ref    =    $_SERVER['HTTP_REFERER'];
        }
        
        $db_sql    =     "INSERT INTO log SET ".
                    "log_type = ? , log_message = ? ".
                    " , log_date = CURDATE() , log_time = CURTIME() , log_ip = ? , log_browser = ? , log_referer = ? ".
                    ", log_uri = ? , log_extra_info = ? ";
        $db_val    =    array(
                            $message_type , $message ,
                            $this->input->ip_address() , $this->input->user_agent() , $ref
                            , $_SERVER['PHP_SELF']  , $extraInfo
                        );
        $db_result = $this->dbw->query($db_sql , $db_val);
    }
    
}
?>

and the library : mmx_log.php
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class MMX_Log{
    var $_ci;
    
    function MMX_Log(){        
        $this->_ci =& get_instance();
        $this->_ci->load->model("Log_model");
    }

    function Log($message = ''   , $message_type = 'Error' ){
        $this->_ci->Log_model->insertLog($message_type , $message);
    }
    
    function file_log($txt){
        if(is_array($txt)){
            $txtContent    =    var_export($txt , true);
        }else{
            $txtContent = $txt;    
        }        
        
        $txtContent .= "\r\n------------------------\r\n";
        
        $FileName        =    '/usr/www/virtual/saved_data.txt';
        if (!$handle = fopen($FileName, 'a')) {    
             exit;
        }        
        if (fwrite($handle, $txtContent) === FALSE) {
            exit;
        }        
        fclose($handle);                
    }    
}
?>

Actually you can get most of it's functionality by using the core CI log system, specially the file part, but as we don interpret the logs for special purposes it really helps.
Our logging system has its own fields which I caught them out as they don't apply here.

Thanks,
MajiD

#9
[eluser]Maglok[/eluser]
As a little sidenote: Profiler has issues with displaying the queries if it is run on PHP4.

As for 2 databases: There is a semi trick. Lets say you have two databases and one is used a lot more then the other, then define that one LAST in the database.php. If you then reference $this->db it will use that database. You would have to load the other database with something like: $this->db2 = $this->load('db2', TRUE); and reference it using $this->db2->something.

I found that I only had to change bits of code where I wanted to approach the 2nd database.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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