You can get the PDO connection by calling ModelClass::connection()
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?
ModelClass::connection()->connection will give you the raw PDO connection
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?
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);
Hi!
Thanks so much for your response. This was enough to get me there.
(1-6/6)
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.