How to make custom sql log in symfony 2 and doctrine 2? - php

I need to add sql logging to work in native WebProfileBundle.
When I do one default connection in application config, I see the sql
queries in my log. But my application uses many connections to many db
servers, so I can't add all the possible connections to config file.
I create runtime connections, i.e.:
$config = array(
'user' => 'user1',
'password' => 'pass1',
'driver' => 'pdo_mysql',
'port' => 3306,
);
$conn = DriverManager::getConnection($config);
then I think, should be command something like this
$conn->getConfiguration()->getSQLLogger($someLoggerObject);
I've tried to solve this problem with DependencyInjection, took
DoctrineBundle as example. But have no luck.
Any help with live code or link to proper documentation would be great

I have this in my conf:
$config = new Configuration;
/** configuration stuff */
$config->setSQLLogger(MyLogger::getInstance());
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => DB_USER,
'password' => DB_PASS,
'host' => DB_HOST,
'dbname' => DB_NAME,
'charset' => 'utf8',
'driverOptions' => array(
'charset' => 'utf8'
)
);
$em = EntityManager::create($connectionOptions, $config);
I use Zend + Doctrine2 and that is in my bootstrap, my logger is a singleton (thats why getInstance). I've never user DriverManager, but i hope this helps.

Related

Silex Doctrine ORM Create Entity

I am trying to connect the doctrine, but I do not quite understand how I can create entities from the console, so for example, to make it easier to create Many to Many
I have already connected the Doctrine itself:
//public/bootstrap/bootstrap.php
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(// Подробнее настройка DBAL тут: http://silex.sensiolabs.org/doc/providers/doctrine.html
'driver' => 'pdo_mysql',
'dbname' => 'test',
'host' => '127.0.0.1',
'user' => 'root',
'password' => 'password',
'charset' => 'utf8'
)
));
there is nothing in the official documentation on how to create entities - https://silex.symfony.com/doc/2.0/providers/doctrine.html
I could not find it on the Internet either, maybe someone knows how to cope with this?

How to setup an SSL encrypted MySQL connection with Doctrine2 in PHP (not Symfony, not Doctrine1)

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',
)
);

Add connection to DBAL dynamically in Silex

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

Google App Engine: How to connect to Cloud SQL with Zend DB

I have a Zend Framework 1.12 application that uses the Zend DB Adapter class to connect with MySql. I need to deploy it soon and am investigating Google App Engine.
My current problem is that I can't connect the application to Google Cloud SQL. This Google page, https://developers.google.com/appengine/docs/php/cloud-sql/, gives an example using PHP and PDO. It uses a DSN as a parameter.
The Zend DB Adapter class does not take a DSN as a parameter, but instead constructs it from the other parameters it receives. I have tried to engineer the Zend DB Adapter params to give the correct DSN, but still not luck. Here's my attempt:
$config = new Zend_Config(
array(
'database' => array(
'adapter' => 'Pdo_Mysql',
'params' => array(
'host' => 'test-instance',
'dbname' => 'my_db_name',
'username' => 'root',
'password' => 'rootpassword',
'charset' => 'utf8',
'driver_options' => [
'unix_socket' => '/cloudsql/test-project'
]
)
)
)
);
try {
$this->_db = Zend_Db::factory($config->database);
$this->_db->getConnection();
} catch (Exception $e){
Zend_Registry::get('logger')->debug('Cannot create the connection...');
Zend_Registry::get('logger')->debug(print_r($e, true));
}
The exception in the message is
The mysql driver is not currently installed
Any ideas?
At this stage I am just trying it from the GAE SDK on my desktop. I have not uploaded the application to the cloud. I doubt this is the issue though. (My workstation IP is authorised to use the Cloud Sql instance).
I have not used the Zend Framework with AppEngine myself, but in my experience (not that I have found it documented anywhere), if you are connecting to Cloud SQL from an AppEngine app using the "root" user, you should not provide the password, or it fails to connect.
Also as shown in the documentation (here: https://developers.google.com/appengine/docs/php/cloud-sql/#PHP_Connecting_to_your_Cloud_SQL_instance), the unix_socket option should be in the format /cloudsql/project-name:instance-name
Therefore I would try rewriting your $config as follows:
$config = new Zend_Config(
array(
'database' => array(
'adapter' => 'Pdo_Mysql',
'params' => array(
'host' => '',
'dbname' => 'my_db_name',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'driver_options' => [
'unix_socket' => '/cloudsql/test-project:test-instance'
]
)
)
)
);

Doctrine2: additional connection options

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).

Categories