Count queries to database in Doctrine2

2019-02-17 02:17发布

How can i get count of queries to database in Doctrine2? I need this just for statistic and to find out more how doctrine work, how much queries generated in different situations. But anyway, how to do this?

2条回答
戒情不戒烟
2楼-- · 2019-02-17 02:54
$stack = new \Doctrine\DBAL\Logging\DebugStack();
$entityManager->getConfiguration()->setSQLLogger($stack);
// do stuff
var_dump($stack);
查看更多
Animai°情兽
3楼-- · 2019-02-17 03:01

Just to add to accepted answer.

To do this from the context of Symfony 2.x controller:

$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);

Thanks to this post: http://vvv.tobiassjosten.net/symfony/logging-doctrine-queries-in-symfony2/

查看更多
登录 后发表回答