I'm using Propel 1.6.x and would like to be able to retrieve the connection name from a Propel Connection object. This is to facilitate the storage of an object in a singleton method, thus:
// If this is called twice with different connections,
// the second one will be wrong
protected function getHashProvider(PropelPDO $con)
{
static $hashProvider;
// Would like to use something like $con->getName() to
// store each instantiation in a static array...
if (!$hashProvider)
{
$hashProvider = Meshing_Utils::getPaths()->getHashProvider($con);
}
return $hashProvider;
}
Since a connection object is instantiated by providing a connection name (or accepting the default name) I'd have thought this would be stored in the object. But a cursory look through the code seems to indicate that it is only used to look up connection details, and is not itself stored.
Is there something I've missed, or should I submit it as a suggestion for Propel2? :)
Right, I've discovered that inside Propel, Propel::getConnection()
doesn't pass the name to the PropelPDO class at all, so there is no way it could contain what I need. Here's how I've fixed it with that limitation in mind.
I've took the view that connections need to have a string identifier, so first I created a new class to wrap the connection:
class Meshing_Database_Connection extends PropelPDO
{
protected $classId;
public function __construct($dsn, $username = null, $password = null, $driver_options = array())
{
parent::__construct($dsn, $username, $password, $driver_options);
$this->classId = md5(
$dsn . ',' . $username . ',' . $password . ',' . implode(',', $driver_options)
);
}
public function __toString()
{
return $this->classId;
}
}
This gives every connection a string representation (to use it, I added a 'classname' key in my runtime XML). Next, I fix up the singleton, thus:
protected function getHashProvider(Meshing_Database_Connection $con)
{
static $hashProviders = array();
$key = (string) $con;
if (!array_key_exists($key, $hashProviders))
{
$hashProviders[$key] = Meshing_Utils::getPaths()->getHashProvider($con);
}
return $hashProviders[$key];
}
Seems to work so far :)