如何在Doctrine2中获取对数据库的查询次数?我只需要统计数据,并了解更多主义如何工作,在不同情况下产生多少查询。但无论如何,怎么做?
答案 0 :(得分:16)
$stack = new \Doctrine\DBAL\Logging\DebugStack();
$entityManager->getConfiguration()->setSQLLogger($stack);
// do stuff
var_dump($stack);
答案 1 :(得分:2)
只是添加到已接受的答案。
要从Symfony 2.x控制器的上下文中执行此操作:
$doctrine = $this->get('doctrine');
$doctrine = $this->getDoctrine();
$em = $doctrine->getConnection();
// $doctrine->getManager() did not work for me
// (resulted in $stack->queries being empty array)
$stack = new \Doctrine\DBAL\Logging\DebugStack();
$em->getConfiguration()->setSQLLogger($stack);
... // do some queries
var_dump($stack->queries);
感谢此帖:http://vvv.tobiassjosten.net/symfony/logging-doctrine-queries-in-symfony2/