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:
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:
Seems to work so far 🙂