I'm setting timezone to php and mysql to internacionalize my CakePHP application.
When the server receives a request from a client, before process request, it connects to a GeoIp location server and gets the Timezone. Then I use date_default_timezone_set() to set php timezone. 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'.
In /lib/Cake/Model/Datasource/Database/Mysql.php I can see at connect() function the following code:
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. 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:
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?
I solved it in the following way.
First of all, adding setOptions() method into DATABASE_CONFIG class as follows:
public function setOptions ($datasource, array $options){
$this->{$datasource} = array_merge($this->{$datasource}, $options);
}
Afterwards, extending ConnectionManager class to initialize it:
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
)
));
Related
I am aware that I can switch this off at mysql level as part of its config. This is limiting as its once per instance, and I dont want to switch on and off/introduce docker to circumvent it.
However, I would like to specify this at configuration level, ie in the array that you pass to doctrine when instantiating the object, so I can have one config that rolls every back at the end of the PHP run:
$conn = array(
'driver' => 'pdo_sqlite',
'path' => __DIR__ . '/db.sqlite',
'autocommit' => false // <- Does this exist?
);
$entityManager = EntityManager::create($conn, $config);
The best I can come up with is to grab the entity manager and switch autocommit off in the setUp() and tearDown() functions, it would be nice to not to have to remember to do that or specify globally. That, and I can do ad-hoc operations outside of phpunit and get the auto rollback.
It's not tested, but you could try the following
$db = new PDO('sqlite:dogsDb_PDO.sqlite', $user, $pass, array(
PDO::ATTR_AUTOCOMMIT => false
));
$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array(
'dbname' => 'mydb',
'user' => 'user',
'password' => 'secret',
'host' => 'localhost',
'pdo' => $db,
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
$entityManager = EntityManager::create($conn, $config);
Source / simillar issue - https://github.com/doctrine/dbal/issues/2315
You could do something like:
/** #var EntityManagerInterface */
private $em;
public function setUp()
{
// initialize the entity manager in some manner .....
$this->em->beginTransaction();
}
public function tearDown()
{
$this->em->rollback();
}
Hope this help
Im creating a Zend APIgility application REST service and having a problem with my fetchAll Mapper function.
I'm connecting to an IBM DB2 database running on an i Series server (AS/400) via DB2 Connect on a Windows Application Server.
My Connection is is made in my local.php as such:
return array(
'db' => array(
'driver' => 'IbmDb2',
'database' => $database,
'username' => $user,
'password' => $password,
'hostname' => $host,
'port' => $port,
'driver_options' => array(
'i5_naming' => DB2_I5_NAMING_ON,
'i5_lib' => 'LIBWEB',
),
),
);
The fetchAll() function in my Mapper class is:
public function fetchAll()
{
$select = new Select('WBRESOURCE');
$paginatorAdapter = new DbSelect($select, $this->adapter);
$collection = new ResourcesCollection($paginatorAdapter);
return $collection;
}
When I hit the DbSelect, ZF2 throws the following DB2 Connect error:
"[IBM][CLI Driver][AS] SQL0204N \"*LIBL.WBRESOURCE\" is an undefined name. SQLSTATE=42704"
Im not sure why its using *LIBL (user defined library list), since I defined the library (SCHEMA) to use as LIBWEB in my connection option.
Thanks in advance!
Rob
Try changing this section:
'driver_options' => array(
'i5_naming' => DB2_I5_NAMING_ON,
'i5_lib' => 'LIBWEB',
Change to":
'driver_options' => array(
'i5_naming' => DB2_I5_NAMING_OFF, <=== change
'i5_lib' => 'LIBWEB',
By using DB2_I5_NAMING_OFF, you should get SQL naming mode. The use of DB2 i5 naming mode will result in things like reliance on a job's library list.
See PHP: db2-connect for some info on the parameter.
I am using ZF2 + Doctrine2 + PHPUNIT, when setting up Phpunit, it works fine for a basic test, however as soon as I try to run a test that invokes Doctrine2, I get:
PDOException: SQLSTATE[HY000] [1045] Access denied for user 'username'#'localhost' (using password: YES)
But, I have never specified "username" nor "localhost" nor any sort of password. In-fact, my application runs perfectly fine, and the configuration I have specifies completely different settings.
So where is PHPUnit getting those settings and how to fix it?
My global.php:
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => '127.0.0.1',
'port' => '3306',
'user' => 'sbapp',
'password' => 'myp#ss',
'dbname' => 'root'
)
)
),
The Test:
class ApplicationControllerTest extends AbstractHttpControllerTestCase
{
protected $traceError = true;
public function setUp()
{
$this->setApplicationConfig(
include '../../../config/application.config.php'
);
parent::setUp();
}
public function testAuthActionCanBeAccessed()
{
$postData = new \stdClass();
$postData->username = "someAppUser";
$postData->password = "12345";
$postData = json_decode(json_encode($postData),true);
$this->dispatch('/auth', 'POST', $postData);
$response = $this->getResponse();
$this->assertResponseStatusCode(200);
$this->assertActionName('auth');
}
}
The relative paths used within the config on setUp when done incorrectly can seriously affect the loading of entities and so on. So I was chasing around the right path.. when calling phpunit from root..or from vendor..or from within the test folder, etc..
Solution:
Call it from project root, and leave the routes exactly as the main project are.
I have ubuntu 10.04 on server.
I am trying to set up the cake php project but it gives me following error
Cake is NOT able to connect to the database.
Datasource class MySQL could not be found.
I have searched lot on the web regarding it.
my config file looks like this
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/MySQL',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'mypassword',
'database' => 'dbname',
'prefix' => '',
//'encoding' => 'utf8',
);
}
I checked that server has all the things set up to connect as PDO I have run following script and it works fine.
$conn = new PDO('mysql:host=localhost;dbname=dbname', $username, $password);
Then further I have changed in Mysql.php file of cake php which is in the "lib\Cake\Model\Datasource\Database"
I tried to give static connection in Mysql.php but this also doesn't work. I did exit in the Mysql.php and seems like control of page is not getting here.
$this->_connection = new PDO('mysql:host=localhost;dbname=dbname', $username, $password);
$this->connected = true;
Please do let me know if I am missing anything.
Thanks in Advance.
Casing matters, it should be:
'datasource' => 'Database/Mysql'
And not:
'datasource' => 'Database/MySQL'
Mysql is not a supported source try 'datasource' => 'Database/Sqlite',
I'm trying to get the memcache query/result cache working with Oracle. It works flawlessly against mysql (verified with memcached console: ./memcached -u nobody -m 40 -vv). Here's what's in web/index.php:
$servers = array(
'host' => 'localhost',
'port' => 11211,
'persistent' => false
);
$cacheDriver = new Doctrine_Cache_Memcache(array(
'servers' => $servers,
'compression' => false,
'prefix' => 'qc-')
);
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_QUERY_CACHE, $cacheDriver);
$manager->setAttribute(Doctrine::ATTR_RESULT_CACHE, $cacheDriver);
$manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE_LIFESPAN, 3600);
This works as supposed against MySQL but fails with following message at the first location where i use ->useResultCache(true):
Result Cache driver not initialized.
Does anyone have clue about what's happening and/or if there's additional configuration needed to get it working with Oracle DB backend?
Thanks.
you should try to set those manager attributes inside your YOUR_APPConfiguration class like explained here : http://www.funstaff.ch/2009/03/19/le-cache-de-resultat-avec-doctrine
class frontendConfiguration extends sfApplicationConfiguration
{
public function configureDoctrine(Doctrine_Manager $manager)
{
$servers = array(
'host' => 'localhost',
'port' => 11211,
'persistent' => false
);
$cacheDriver = new Doctrine_Cache_Memcache(array(
'servers' => $servers,
'compression' => false,
'prefix' => 'qc-')
);
$manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
$manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE_LIFESPAN, 3600);
$manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE, $cacheDriver);
$manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE_LIFESPAN, 3600);
}
}