Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the SQL for a Doctrine 2 entity insert

Tags:

doctrine-orm

Is it possible to get the SQL for a Doctrine 2 entity insert?

For example, let's say I have this:

$foo = new Foo();
$foo->setName('bar');
$em->persist($foo);
$em->flush();

Is there a way to get the SQL for the INSERT statement there? If so, how?

like image 361
Jason Swett Avatar asked Aug 28 '12 19:08

Jason Swett


2 Answers

You could try the SQL-Logger:

$em->flush(); // to write cached stuff down and isolate the following

// activate logger
$em->getConnection()
    ->getConfiguration()
    ->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());

$foo = new Foo();
$foo->setName('bar');
$em->persist($foo);
$em->flush();

// disable logger
$em->getConnection()
    ->getConfiguration()
    ->setSQLLogger(null);

It works with other DBMS than MySQL too. There are also other possibilities to write in files. Look here:

https://www.brunsware.de/blog/symfony/monolog-doctrine-logfile.html

http://vvv.tobiassjosten.net/symfony/logging-doctrine-queries-in-symfony2/

like image 68
FlorIT Avatar answered Dec 12 '22 06:12

FlorIT


You could activate the query log in your MySQL server (guessing it's it) : in /etc/mysql/my.ini (usual place) :

[mysqld]
log=/tmp/mysql.log

then read this file where you will find each issued query.

like image 37
Parallelis Avatar answered Dec 12 '22 08:12

Parallelis