9

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 Answers 2

21
$stack = new \Doctrine\DBAL\Logging\DebugStack();
$entityManager->getConfiguration()->setSQLLogger($stack);
// do stuff
var_dump($stack);
Sign up to request clarification or add additional context in comments.

Comments

4

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/

1 Comment

$doctrine->getManger() did not work for me Is it because of the missing a?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.