0

I'm using ActiveRecord and I need switching databases. When I log-in I select a database. The databases are the same schema. I tried:

$connections = array(
   '1' => 'mysql://root:pass@localhost/db1;charset=utf8',
   '2' => 'mysql://root:pass@localhost/db2;charset=utf8',
   'test' => 'mysql://root:password@localhost/database_name'
 );

 $current_db = $_SESSION['db'] ?  $_SESSION['db'] : '2';


 ActiveRecord\Config::initialize(function($cfg) use ($connections, $current_db)
 {
   $cfg->set_model_directory(MODEL_PATH);
   $cfg->set_connections($connections);

   $cfg->set_default_connection($current_db);
 });

db '2' is default. But does not work.

Charles
  • 50,943
  • 13
  • 104
  • 142
Cristhian Boujon
  • 4,060
  • 13
  • 51
  • 90
  • What kind of error do you get? – axel.michel Dec 27 '12 at 13:03
  • There is not error. The problem is that the database does not change even $current_db take any value. – Cristhian Boujon Dec 27 '12 at 13:23
  • 1
    the code you posted here looks good, it must be something else (setting the connection elsewhere, some value in $_SESSION['db'] etc) – Bogdan D Dec 27 '12 at 15:58
  • first, do some debugging. Echo your `$current_db` for instance in various places confirming that it is actually "2". Then, check if it does work with another default (`test` or `1`). Also, test your connection apart from activerecord. Then come back here and show the results :) – Nanne Dec 28 '12 at 08:13
  • I added `print_r($_SESSION);` but does not show anything (?!?!?!) if I add the line `print_r(Array(123, 55, 66))` shows `Array ( [0] => 123 [1] => 55 [2] => 66 )` Why? – Cristhian Boujon Jan 02 '13 at 12:34

2 Answers2

0

I generally use a method like this;

$all = new SomeModel(); // class of ActiveRecord\Model
$all->table()->class->setStaticPropertyValue("connection","test"); //test - defined in cfg
$all->table()->reestablish_connection();
//then use it as usual.
$all_data = $all->all();
0

I found a link with an excellent solution for switching databases here

EDIT: I define a method called switch_connection in my model

   public static function switch_connection($name) {

     $cfg = ActiveRecord\Config::instance();    
     $valid = $cfg->get_connections();
     if ( ! isset($valid[$name])) {
       throw new ActiveRecord\DatabaseException('Invalid connection specified');
     }

     // Get the name of the current connection
     $old = self::$connection;

     $cm = ActiveRecord\ConnectionManager::instance();
     $conn = $cm::get_connection($name);
     static::table()->conn = $conn;

     return $old;
   }

If you see in the link, note that I added the static keyword. I think that is better idea.

Cristhian Boujon
  • 4,060
  • 13
  • 51
  • 90
  • Please note that you should post the useful points of an answer here, on this site, or your post risks being deleted as ["Not an Answer"](http://meta.stackexchange.com/q/8259). You may still include the link if you wish, but only as a 'reference'. The answer should stand on its own without needing the link. – Andrew Barber Jan 30 '13 at 20:49