How can I set custom driver options in Symfony2?
In this case I want to run SET time_zone = '-04:30'
in MySQL as part of the connection initialization.
Set in app/config/config.yml
the following:
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
# Here we configure the custom driver options
# In this case is PDO::MYSQL_ATTR_INIT_COMMAND
# The constant can not be used in this context
options: {1002: "SET time_zone = '-04:30'"}
Unlike Symfony 1.4
, there isn't a default_timezone
config parameter to set the default timezone in Symfony2. I'm not sure about the reasons for this, but if you like keeping as much of your configuration within the app, then use the code below. Set the default timezone (and any similar PHP configuration) by overriding the AppKernel#init()
function. Remember to call its parent.
<?php
// AppKernel.php
class AppKernel extends Kernel
{
//..
//.......
//...........
// --- Append the init function below ---
public function init()
{
date_default_timezone_set( 'Asia/Kolkata' );
parent::init();
}
}
Or you can edit php.ini
datetime = 'Asia/Kolkata'
Note: init method was deprecated since version 2.3, to be removed in 3.0. You must move your logic in the constructor instead:
public function __construct($environment, $debug)
{
date_default_timezone_set('Asia/Kolkata');
parent::__construct($environment, $debug);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.