开发者

How to display the SQL query that causes a PDOException in Doctrine

开发者 https://www.devze.com 2023-03-25 11:02 出处:网络
I am playing with Doctrine 2 and PHPUnit and of course, something goes wrong sometimes. That\'s what unittesting is all about. But when I get a PDOException for one reason or the other, I don\'t get t

I am playing with Doctrine 2 and PHPUnit and of course, something goes wrong sometimes. That's what unittesting is all about. But when I get a PDOException for one reason or the other, I don't get to see the query that is causing it. Example:

$ phpunit --group database
PHPUnit 3.5.14 by Sebastian Bergmann.

E

Time: 2 seconds, Memory: 14.75Mb

There was 1 error:

1) My\Tests\Entity\MyMappingTest::testLoad
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't11.id' in 'on clause'

/usr/local/svn/php5/libraries/Doctrine/DBAL/Connection.php:613
/usr/local/svn/php5/libraries/Doctrine/ORM/Persisters/BasicEntityPersister.php:569
/usr/local/svn/php5/lib开发者_如何学Craries/Doctrine/ORM/Persisters/BasicEntityPersister.php:624
/usr/local/svn/php5/libraries/Doctrine/ORM/UnitOfWork.php:1979
/usr/local/svn/php5/libraries/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php:139
/usr/local/svn/php5/libraries/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php:44
/usr/local/svn/php5/libraries/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:99
/usr/local/svn/php5/libraries/Doctrine/ORM/Persisters/BasicEntityPersister.php:580
/usr/local/svn/php5/libraries/Doctrine/ORM/EntityRepository.php:130
/usr/local/svn/php5/libraries/Doctrine/ORM/EntityManager.php:350


FAILURES!
Tests: 1, Assertions: 1, Errors: 1.

That's all fine and dandy, but how do I tell Doctrine or PHPUnit to give me the SQL query that is causing this exception so that I have any hope of debugging it? Right now, this exception is kind of useless. I tried var_dumping the exception object but that just recurses forever. Surely I should be able to tell Doctrine to give me more information?


You could enable native Doctrine SQL logging, or with a bit more effort you could implement Doctrine\DBAL\Logging\SQLLogger to store the last query and override PHPUnit_Framework_TestCase::onNotSuccessfulTest() to add it a new exception in the case of a PDOException. Unfortunately, you'll lose the original stack trace, but PHP doesn't allow changing an exception's message once it's created. :(

Use setSQLLogger() on the config object to use your logger. Do this in PHPUnit bootstrap.php and make it available to your test case base class.

0

精彩评论

暂无评论...
验证码 换一张
取 消