Laravel 4-无法在种子或迁移类中捕获数据库异常 [英] Laravel 4 - Cannot catch database exception in seed or migration class
问题描述
带有MySql数据库的Laravel 4. 由于某种原因,我无法在种子或迁移类中捕获数据库异常( Illuminate \ Database \ QueryException ):代码从不进入catch块.
Laravel 4 with MySql db. For some reason, I cannot catch DB exceptions (Illuminate\Database\QueryException) inside a seed or migration class: the code never enters the catch block.
例如,如果我尝试在名称"列为UNIQUE的表上插入:
For example, if I try to insert on a table where the column 'name' is UNIQUE:
try {
$data = array('id' => 1, 'name' => 'foo');
DB::table('table')->insert($data);
}
catch (\Exception $e) {
$this->command->error("SQL Error: " . $e->getMessage() . "\n");
}
...我总是会收到此错误:
...I always get this error:
PHP Warning: Uncaught exception 'Illuminate\Database\QueryException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
- 我试图捕获Exception,\ Exception,\ PDOException,\ Illuminate \ Database \ QueryException等,但是我没有运气.
- 我可以捕获其他类型的异常(例如,除以零等)
- 我可以在route.php中捕获QueryException,相同的代码也可以正常工作:代码输入"catch",我收到消息"SQL错误:等".但没有提出警告(正确)
有什么主意吗? 感谢您的帮助.
Any idea? Thanks for your help.
推荐答案
Thanks to php exceeds execution time on throwing an exception in recursion I finally understood the problem.
显然,它与XDebug有关,并且仅在Mac OS X上的PHP上发生(我在Windows上尝试了多个PHP,但从未遇到此问题),并且仅在通过CLI运行迁移或种子时(例如php artisan db) :seed --class = className).
Apparently it's related to XDebug, and it occurs only on PHP on Mac OS X (I tried several PHPs on Windows, but never ran into this problem), and only when running a migration or seed by CLI (e.g. php artisan db:seed --class=className).
它与以下XDebug设置有关:
It's related to these XDebug settings:
xdebug.var_display_max_depth = -1
xdebug.var_display_max_children = -1
xdebug.var_display_max_data = -1
我不知道为什么,但是这导致QueryException永远不会被捕获,PHP向我展示了整个异常堆栈.
I don't exactly know why, but this caused QueryException to never be catched, and PHP showed me the entire exception stack.
如果我注释了这些选项,或者将它们设置为更合适的值(例如10),或者如果我禁用了XDebug(不是一个好的解决方案),则一切运行正常,并且在迁移/种子中,我可以捕获QueryExceptions.
If I comment these options, or set them to a more appropriate value (e. g. 10) or if I disable XDebug (not a good solution), everything runs ok, and inside migrations/seeds I can catch QueryExceptions.
这篇关于Laravel 4-无法在种子或迁移类中捕获数据库异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!