Sara Keesler Fri Aug 27 14:55:18 -0400 2010

Subject: Using an alternate DB connection

Hi!

Just starting to implement php-activerecord into our application. So far it's looking like it could be really useful to us. At the moment we create a database connection using PDO. Can we use this db connection, or do we need to create a separate, parallel connection for php-AR?

If you need more details, let me know.


Kien La Mon Aug 30 18:44:56 -0400 2010

You can get the PDO connection by calling ModelClass::connection()

Sara Keesler Tue Aug 31 12:52:26 -0400 2010

Thanks for the response! I can see how to get the AR connection via the connection() call, but I don't see how we can use an already-instantiated PDO. Is there any way to do that?

Kien La Tue Aug 31 13:00:06 -0400 2010

ModelClass::connection()->connection will give you the raw PDO connection

Sara Keesler Tue Aug 31 14:05:48 -0400 2010

I am not sure I'm understanding what you're saying here. Are we able to set that property directly, like:

ModelClass::connection()->connection = $ourPredefinedPDO;

?

I've been trying to extend the config class so that it will take a PDO instead of using the connection strings, but it seems to be a fairly large hack to do something fairly simple. Am I missing something?

Jacques Fuentes Tue Aug 31 18:40:35 -0400 2010

If you're trying to use the same raw PDO connection instance, then it would be easier if you did something like the following:

$connection = ModelClass::connection()->connection;
// now pass $connection along to your other code
SomeCode::setConnection($connection);

This way you're using the PDO connection from php-AR, retrieving it, and then passing it to your other code. If you needed to do the reverse of this, you would need to extend the ConnectionManager class and add a set_connection method like so

// lib/ConnectionManager.php
class ConnectionManager extends Singleton
{
  // existing code

  // $name would be the connection string you use in your php-AR config settings
  // $pdo_object is your $ourPredefinedPDO
  public static function set_connection($name, $pdo_object) 
  {
     self::$connections[$name] = $pdo_object;
  }
}

// in your config file

ActiveRecord\ConnectionManager::set_connection('some_name', $ourPredefinedPDO);

Sara Keesler Thu Sep 02 19:51:21 -0400 2010

Hi!

Thanks so much for your response. This was enough to get me there.

(1-6/6)