我使用的是Propel 1.6.x,希望能够从Propel connection对象中检索连接名称。这是为了方便对象在单例方法中的存储,因此:
// 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?:)
发布于 2011-11-13 21:20:42
是的,我发现在Propel中,Propel::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‘键)。接下来,我将修复单例,如下所示:
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];
}到目前为止似乎还不错:)
https://stackoverflow.com/questions/8111231
复制相似问题