Is there a way to add some additional parameters to the database connection in Doctrine2?
For example I need to enable network communication compression.
$DoctrineConnectionOptions = array(
'driver' => 'pdo_mysql',
'dbname' => 'db',
'user' => 'root',
'password' => 'root',
'host' => 'localhost',
/*
additional_options => array(
PDO::MYSQL_ATTR_COMPRESS => true
)
*/
);
$em = \Doctrine\ORM\EntityManager::create($DoctrineConnectionOptions, $DoctrineConfig);
Check EntityManager::create() source code. You can directly pass customly made PDO object as a connection option ('pdo' key).
Related
I have been trying to solve this issue already for a few days without any results. When using default php pdo object I can connect to the database:
$db = new \PDO('mysql:unix_socket=/cloudsql/project-id:database-instance;dbname=test',
'root', // username
'' // password
);
But when trying to connect with ZF2 adapter the connection just times out.
'db' => array(
'driver' => 'Pdo_Mysql',
'database' => 'test',
'username' => 'root',
'unix_socket' => '/cloudsql/project-id:database-instance',
),
I am quite sure that the problem is somehow with the unix_socket as I can connect to the Cloud SQL server from my localhost directly without socket:
'db' => array(
'driver' => 'Pdo_Mysql',
'host' => 'xxx.xxx.xxx.xxx',
'database' => 'test',
'username' => 'user',
'password' => 'password',
),
What I am missing?
unix_socket isn't a recognized option for the pdo driver (https://github.com/zendframework/zf2/blob/master/library/Zend/Db/Adapter/Driver/Pdo/Connection.php#L164). Try this instead
'db' => array(
'dsn' => 'mysql:unix_socket=/cloudsql/project-id:database-instance;dbname=test',
'username' => 'user',
'password' => 'password',
)
I'm digging into ZF2, and I've run into some confusion on how to use Zend\Config with Zend\Db to manually set up a connection.
In different places in the manual, there are db configs in different formats.
This one shows a flat array as the config format:
https://packages.zendframework.com/docs/latest/manual/en/modules/zend.db.adapter.html
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'Mysqli',
'database' => 'zend_db_example',
'username' => 'developer',
'password' => 'developer-password'
));
While this one shows a nested format:
https://packages.zendframework.com/docs/latest/manual/en/modules/zend.config.introduction.html
$configArray = array(
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
);
What I expect to happen is that I can call for a new db adapter like so, but this throws exceptions:
$config = new Zend\Config\Config(
array(
'db' => array(
'adapter' => 'Mysqli',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
)
);
$adapter = new Zend\Db\Adapter\Adapter($config->db);
What I end up having to do is:
$config = new Zend\Config\Config(
array(
'db' => array(
'driver' => 'Mysqli',
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'database' => 'mydatabase'
)
)
);
$adapter = new Zend\Db\Adapter\Adapter($config->db->toArray());
Is there a better way of achieving what I'm trying to achieve without having to resort to the service manager?
Ignore the example from the Zend Config introduction page, that's just showing how to make a config object from a PHP array, the structure of the array isn't meant to show anything in particular.
Since you don't want to use the service manager, you need to pass the parameters to the adapter class in the structure it expects. It expects an array, a config object won't work. You've worked out what the structure of the array is, so that's what you need to use.
I think this page in the docs: http://framework.zend.com/manual/2.3/en/tutorials/tutorial.dbadapter.html (the "Basic setup" section) gives a better explanation of the service manager approach, which is how I'd do it in an MVC app at least.
I am having a hard time finding documentation / examples of how to setup an SSL encrypted connection with Doctrine2 to MySQL. I'm not using Symfony, so looking for the pure PHP path.
What I'm stuck on is basically how to convey the MYSQL_CLIENT_SSL (or MYSQLI_CLIENT_SSL) flag, and the path to the ca certificate. I can live with not verifying the certificate, but I can't live with not encrypting the connection for this task.
On the command line this would be done similar to this:
mysql --ssl-verify-server-cert --ssl-ca=/mysql-ssl-certs/ca-cert.pem --ssl -h host [etc]
In pure php using the mysql extension I think it would look something like:
$conn = mysql_connect($host, $user, $pass, false, MYSQL_CLIENT_SSL);
With mysqli (i think) it would be something like this:
$db = mysqli_init();
$db->ssl_set(null, null, $cert, null, null);
$db->real_connect($host, $user, $pass, $dbname);
The question is, how do I do this with Doctrine2? Is it even possible? How do I modify the initialization for Doctrine2 to do this?
$DOCTRINE2_DB = array(
'driver' => 'pdo_mysql',
'host' => $host,
'user' => $user,
'password' => $pass,
'dbname' => $dbname,
'unix_socket' => $sockpath,
);
$DOCTRINE2_EM = \Doctrine\ORM\EntityManager::create($DOCTRINE2_DB, $DOCTRINE2_CONFIG);
$EM =& $DOCTRINE2_EM; // for brevity & sanity
You should be able to add an additional parameter driverOptions and set the appropiate SSL configuration for PDO
http://es1.php.net/manual/es/ref.pdo-mysql.php#pdo-mysql.constants
$DOCTRINE2_DB = array(
'driver' => 'pdo_mysql',
'host' => $host,
'user' => $user,
'password' => $pass,
'dbname' => $dbname,
'unix_socket' => $sockpath,
'driverOptions' => array(
PDO::MYSQL_ATTR_SSL_CA => '...',
PDO::MYSQL_ATTR_SSL_CERT => '...',
PDO::MYSQL_ATTR_SSL_KEY => '...'
)
);
I can't test it but looking at the code here I think it should work
[EDIT BY ASKER:]
Here is how it worked for me:
$DOCTRINE2_DB = array(
'driver' => 'pdo_mysql',
'host' => $host,
'user' => $user,
'password' => $pass,
'dbname' => $dbname,
'unix_socket' => $sockpath,
'driverOptions' => array(
PDO::MYSQL_ATTR_SSL_CA => '/file/path/to/ca_cert.pem',
)
);
I am writing a PHP application using the Silex framework. I'm using the Doctrine Service Provider, and I can open a connection normally as this:
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'dbs.options' => array (
'localhost' => array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'test',
'user' => 'root',
'password' => 'root',
'charset' => 'utf8',
)
),
));
That works perfectly. What I want now is to add another database connection afterwards in my code. I know I can do it adding another element to dbs.options, but I want to do it afterwards, in the controllers (as different controllers will use different database connections).
Is that possible? I guess I could use something like DriverManager::getConnection($options, $config, $manager); but there's probably a better way to do it.
Thanks!
$conn = DriverManager::getConnection($params, $config);
this is original code to generate new connection, so what you wrote is ok
Link: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html
You can configure multiple db connections using the DoctrineServiceProvider bundled with Silex.
Replace the db.options with an array of configurations where keys are connection names and values configuration options.
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'dbs.options' => array (
'mysql_read' => array(
'driver' => 'pdo_mysql',
'host' => 'mysql_read.someplace.tld',
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
'charset' => 'utf8',
),
'mysql_write' => array(
'driver' => 'pdo_mysql',
'host' => 'mysql_write.someplace.tld',
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
'charset' => 'utf8',
),
),
));
Access multiple connections in your controllers:
$app->get('/blog/{id}', function ($id) use ($app) {
$sql = "SELECT * FROM posts WHERE id = ?";
$post = $app['dbs']['mysql_read']->fetchAssoc($sql, array((int) $id));
$sql = "UPDATE posts SET value = ? WHERE id = ?";
$app['dbs']['mysql_write']->executeUpdate($sql, array('newValue', (int) $id));
return "<h1>{$post['title']}</h1>".
"<p>{$post['body']}</p>";
});
Source: http://silex.sensiolabs.org/doc/providers/doctrine.html
I am creating one database wrapper class and want to have a feature to connect with multiple database using PDO. I have connected multiple database using pdo as below.
$config = array(
'database1' => array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'users',
'port' => '',
'dbtype' => 'mysql'
),
'database2' => array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'directory',
'port' => '',
'dbtype' => 'mysql'
),
);
foreach ($config as $key => $value)
{
$this->conn[$value['database']] = new PDO("mysql:host=".$value['hostname'].";dbname=".$value['database'], $value['username'],$value['password']);
}
Is it good practice to connect multiple databases as above using any types of database (may using Oracle or Mysql or may be both same). Because my above code takes more time in loading.
I want to have a best practice as Yii or Symphony framework uses to connect db.
Can anybody have solution.
Thanks