Php 我可以从推进连接获取连接名称吗?

Php 我可以从推进连接获取连接名称吗?,php,connection,propel,Php,Connection,Propel,我正在使用Propel 1.6.x,希望能够从Propel连接对象检索连接名称。这是为了便于在单例方法中存储对象,因此: // If this is called twice with different connections, // the second one will be wrong protected function getHashProvider(PropelPDO $con) { static $hashProvider; // Would like to u

我正在使用Propel 1.6.x,希望能够从Propel连接对象检索连接名称。这是为了便于在单例方法中存储对象,因此:

// 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;
}
由于连接对象是通过提供连接名称(或接受默认名称)来实例化的,所以我认为这将存储在对象中。但是粗略地看一下代码似乎表明它只用于查找连接详细信息,而本身并没有存储


我是否遗漏了什么,还是应该将其作为建议提交给Propel2?:)

没错,我发现在spreep内部,
spreep::getConnection()
根本不会将名称传递给PropelPDO类,因此它无法包含我需要的内容。下面是我如何在考虑到这个限制的情况下修复它的

我认为连接需要有一个字符串标识符,所以首先我创建了一个新类来包装连接:

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;
    }
}
这为每个连接提供了一个字符串表示(为了使用它,我在运行时XML中添加了一个“classname”键)。接下来,我修复singleton,因此:

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];
}

似乎到目前为止还有效:)

对,我发现在spreep内部,
spreep::getConnection()
根本没有将名称传递给PropelPDO类,因此它无法包含我需要的内容。下面是我如何在考虑到这个限制的情况下修复它的

我认为连接需要有一个字符串标识符,所以首先我创建了一个新类来包装连接:

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;
    }
}
这为每个连接提供了一个字符串表示(为了使用它,我在运行时XML中添加了一个“classname”键)。接下来,我修复singleton,因此:

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];
}
到目前为止似乎有效:)