Michele Madeddu Fri Apr 13 08:24:07 -0400 2012

Subject: closing db connection

Hi to all,
anyone knows how to close the db connection?
Everytime I include classes connecting to different databases, I get an error from the second one...saying me that the user set in the first class obviously cannot connect to the db defined in the second...so I need to close the first connection inside the class.

Thanks
Michele


Mr. Carl Sat Apr 14 18:15:36 -0400 2012

We need more information like how you have the database config define, model coding, etc.

 1 class Federal_Taxes extends ActiveRecord\Model
 2 {
 3     static string $connection = 'federal';
 4 }
 5 
 6 class State_Taxes extends ActiveRecord\Model
 7 {
 8     static string $connection = 'state';
 9 }

In this case the model for federal taxes will access the federal database and state taxes will access the state database.

I'm using CodeIgniter so replacing Quick Start define database connection with the code below I can use CodeIgniter database config infor

 1 
 2 <?php defined('BASEPATH') OR exit('No direct script access allowed');
 3 /**
 4 * Init for php.activerecord
 5 * “Activerecord.php” to act as the bootstrapper for PHP.ActiveRecord
 6 */
 7 
 8 // Load php.activerecord
 9 require_once APPPATH.'/libraries/php-activerecord/1.0.0/ActiveRecord.php';
10 
11 // Load CodeIgniter's Model class 
12 require_once BASEPATH.'core/Model.php';
13 
14 class Activerecord 
15 {    
16     function __construct() 
17     {
18         // Load database configuration from CodeIgniter
19         include APPPATH.'/config/database.php';
20 
21         // Build/Format connections list from database.php
22         $dsn = array();
23 
24         if(is_array($db)) // Check if exist and its array
25         {
26             foreach($db as $groupName => $db_values)
27             {
28                 // Covert db to dsn format for all group
29                 $dsn[$groupName] = $db[$groupName]['dbdriver'] .
30                     '://'   . $db[$groupName]['username'] .
31                     ':'     . $db[$groupName]['password'] .
32                     '@'     . $db[$groupName]['hostname'] .
33                     '/'     . $db[$groupName]['database'];
34             }
35         }
36         else
37         {
38             //throw error because it wasn't set
39         }
40 
41         // Initialize ActiveRecord
42         ActiveRecord\Config::initialize
43         (
44             function($cfg) use ($dsn, $active_group)
45             {
46                 $cfg->set_model_directory(APPPATH.'/models');
47                 $cfg->set_connections($dsn); // Passing ALL database connection config
48                 $cfg->set_default_connection($active_group); // 
49             }
50         );
51     }
52 }
53 /* End of file Activerecord.php */
54 /* Location: ./application/libraries/Activerecord.php */

I haven't tested this code

Yoan B Sun Apr 15 07:54:30 -0400 2012

PHP AR uses PDO (http://php.net/pdo) and there are no methods to close the db connection. Your problem might be elsewhere, database system usually accepts more than one connection at the time ;-)

(1-2/2)