代码之家  ›  专栏  ›  技术社区  ›  David Weinraub

使用条令1.2和Zend框架设置mysql会话变量-时区

  •  1
  • David Weinraub  · 技术社区  · 14 年前

    获得了一个Zend框架Web应用程序,使用条令1.2连接到MySQL5.1服务器。

    大多数数据需要在本地时区中输入和显示。所以听从建议 here ,我想将php和mysql设置为使用UTC,然后在插入/更新之前处理到本地时间的显示转换和到UTC的转换。[如果这完全是愚蠢的,我很高兴听到更好的方法。]

    那么,我如何告诉Distrine将MySQL会话设置为UTC?本质上,我该如何告诉条令发布mysql命令? SET SESSION time_zone = 'UTC'; 当它打开连接时?

    事先谢谢!

    1 回复  |  直到 14 年前
        1
  •  2
  •   David Weinraub    14 年前

    Doctrine_Connection Doctrine_EventListener postConnect()

    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 site