看起來這可以通過使用postConnect()
方法附加到Doctrine_Connection
對象Doctrine_EventListener
來完成。
Doctrine ORM for PHP - Creating a New Listener
喜歡的東西:
class Kwis_Doctrine_EventListener_Timezone extends Doctrine_EventListener
{
protected $_timezone;
public function __construct($timezone = 'UTC')
{
$timezone = (string) $timezone;
$this->_timezone = $timezone;
}
public function postConnect(Doctrine_Event $event)
{
$conn = $event->getInvoker();
$conn->execute(sprintf("SET session time_zone = '%s';", $this->_timezone));
}
}
[使用sprintf()
這裏可能是業餘的,但我無法弄清楚如何做一個適當的參數綁定。 (不好意思的笑臉)
然後在我的應用程序的Bootstrap.php
,我有這樣的事情:
protected function _initDoctrine()
{
// various operations relating to autoloading
// ..
$doctrineConfig = $this->getOption('doctrine');
$manager = Doctrine_Manager::getInstance();
// various boostrapping operations on the manager
// ..
$conn = Doctrine_Manager::connection($doctrineConfig['dsn'], 'doctrine');
// various boostrapping operations on the connection
// ..
// Here's the good stuff: Add the EventListener
$conn->addListener(new Kwis_Doctrine_EventListener_Timezone());
return $conn;
}
[一個稍微題外話注:我的本地開發機器上,我不停地奔跑到MySQL錯誤我沒有進入任何時區似乎被接受。原來,標準的MySQL安裝會創建mysql
數據庫中的所有時區表,但實際上並未填充它們;你需要分別填充它們。更多信息在MySQL site。]
你會考慮用`--timezone = UTC`來啓動mysql嗎? http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html?否則,您需要查找http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_query.html。 – ajreal 2010-11-26 05:17:19