开发者

How to set database time zone in application.ini

开发者 https://www.devze.com 2023-01-31 13:59 出处:网络
I have an application in Zend Framework that has to run for a different time zone than the server. Is there an option to set the database server (MySQL in this case) time zone in application.ini?

I have an application in Zend Framework that has to run for a different time zone than the server. Is there an option to set the database server (MySQL in this case) time zone in application.ini?

My current options are:

resources.db.adapter = "Pdo_Mysql"
resources.db.params.charset = "utf8"
resources.db.params.driver_options.1002 = "SET NAMES utf8"
resources.db.params.host = "localhost"
resources.db.params.username = "usernam"
resources.db.params.password = "password"
resources.db.params.dbname = "databasename"

I know I can do something like SET timezone = 'Europe/London', but I really need to do it in the config file.

EDIT

Googling around I found that resources.db.params.driver_options.1002 should be setting the value for PDO::MYSQL_ATTR_INIT_COMMAND.

Therefore resources.db.params.driver_options.1002 = "SET NAMES utf8, time_zone = 'Europe/London'" should do the trick. But no luck so far.

FINAL EDIT

Found it. After a lot of searching and debugging I found the following code at Zend\Db\Adapter\Pdo\Mysql.php

if (!empty($this->_config['charset'])) {
    $initCommand = "SET NAMES '" . $this->_config['charset'] . "'";
    $this->_config['driver_options'][1002] = $initCommand; // 1002 = PDO::MYSQL_ATTR_INIT_COMMAND
}

As I have resources.db.params.charset = "utf8" in my applica开发者_运维百科tion.ini, it was overwriting PDO::MYSQL_ATTR_INIT_COMMAND.

Deleting that line solved it.


I'm answering my own question just to close this, as the response is already in one of the edits.

resources.db.params.charset

and

resources.db.params.driver_options.1002

as I had in my application.ini cannot be used together, as resources.db.params.charset ovewirites driver_options.1002.

If you need to set the timezone, remove resources.db.params.charset and pass the timezone in the charset in driver_options. E.g.:

resources.db.params.driver_options.1002 = "SET NAMES utf8, time_zone = 'Europe/London'"


This works for me:

resources.db.adapter = "Pdo_Mysql"
resources.db.params.driver_options.1002 = "SET NAMES 'utf8', CHARACTER SET 'utf8', time_zone = 'Europe/London'"


you might as well consider adding it on your bootstrap:

<?php 
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    protected function _initTimeZone() {
        date_default_timezone_set('Asia/Manila');
    }
}


Try build pre dispatch plugin and get data from config file for query

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号