简体   繁体   English

Cakephp设置数据库时区

[英]Cakephp set database timezone

I'm setting timezone to php and mysql to internacionalize my CakePHP application. 我将时区设置为php和mysql,以对我的CakePHP应用程序进行国际化。

When the server receives a request from a client, before process request, it connects to a GeoIp location server and gets the Timezone. 当服务器在处理请求之前收到客户端的请求时,它将连接到GeoIp位置服务器并获取时区。 Then I use date_default_timezone_set() to set php timezone. 然后,我使用date_default_timezone_set()设置php时区。 The problem comes up when I want to set database timezone. 我要设置数据库时区时出现问题。 Once Cakephp connected, I need to execute sql query like SET time_zone='-06:00' . 一旦Cakephp连接,我需要执行SQL查询,例如SET time_zone='-06:00'

In /lib/Cake/Model/Datasource/Database/Mysql.php I can see at connect() function the following code: 在/lib/Cake/Model/Datasource/Database/Mysql.php中,我可以在connect()函数中看到以下代码:

    try {
        $this->_connection = new PDO(
            $dsn,
            $config['login'],
            $config['password'],
            $flags
        );
        $this->connected = true;
        if (!empty($config['settings'])) {
            foreach ($config['settings'] as $key => $value) {
                $this->_execute("SET $key=$value");
            }
        }
    } catch (PDOException $e) {
        throw new MissingConnectionException(array(
            'class' => get_class($this),
            'message' => $e->getMessage()
        ));
    }

There is a $config['settings'] array that can be configured to do it. 有一个$ config ['settings']数组可以配置为执行此操作。 But I don't know how to fill settings array and where it's the best place to do that. 但是我不知道如何填充设置数组以及在哪里做是最好的选择。

What I need is modify default datasource config on-the-fly 我需要的是即时修改默认数据源配置

You can add an additional key to config array located at app/Config/database.php like this: 您可以像下面这样向位于app/Config/database.php配置数组添加一个附加键:

public $default = array(
    'datasource'  => 'Database/Mysql',
    'persistent'  => false,
    'host'        => 'localhost',
    'login'       => 'db_user',
    'password'    => 'db_pass',
    'database'    => 'db_name',
    'prefix'      => '',
    'settings'    => array(
           'time_zone' => "'+01:00'", // note the quotes!
    )
);

Related: CakePHP switch database (using same datasource) on the fly? 相关: CakePHP动态切换数据库(使用相同的数据源)?

I solved it in the following way. 我通过以下方式解决了它。

First of all, adding setOptions() method into DATABASE_CONFIG class as follows: 首先,将setOptions()方法添加到DATABASE_CONFIG类中,如下所示:

public function setOptions ($datasource, array $options){
    $this->{$datasource} = array_merge($this->{$datasource}, $options);
}

Afterwards, extending ConnectionManager class to initialize it: 然后,扩展ConnectionManager类以对其进行初始化:

class ConnectionManagerCustomConfig extends ConnectionManager
{
    public static function initialize(){
        self::_init();
    }
}

Now I initialize the class and add new options: 现在,我初始化类并添加新选项:

ConnectionManagerCustomConfig::initialize();
$configClass = ConnectionManagerCustomConfig::$config;
$configClass->setOptions('default', array(
  'settings' => array(
    'time_zone' => $offset
  )
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM