<?php
use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\Common\Cache\ArrayCache;
use PHPSnippets\Db\Entity\Test;
// config Doctrine
$config = $entityManager->getConnection()
->getConfiguration();
// set Logger
$logger = new DebugStack();
$config->setSQLLogger($logger);
// set Result Cache; ArrayCache is actually default
$config->setResultCacheImpl(new ArrayCache());
// execute same query
$entityManager->find(Test::CLASS, 2);
$entityManager->find(Test::CLASS, 2);
$entityManager->find(Test::CLASS, 2);
// same query as before
$entityManager->getRepository(Test::CLASS)
->find(2);
// actually the same query, but Doctrine
// uses other aliases
$qb = $entityManager->createQueryBuilder();
$qb->select('t')
->from(Test::CLASS, 't')
->where('t.id = :id')
->setParameters(['id' => 2])
->getQuery()
->getResult();
// show all executed queries, no cached ones
foreach ($logger->queries as $log) {
echo sprintf(
'<b>query:</b> %s, <b>params:</b> [%s]' . PHP_EOL . PHP_EOL,
$log['sql'],
implode($log['params'])
);
}
Result
query: SELECT t0.id AS id_1, t0.name AS name_2, t0.creation_date AS creation_date_3 FROM test t0 WHERE t0.id = ?, params: [2]
query: SELECT t0_.id AS id_0, t0_.name AS name_1, t0_.creation_date AS creation_date_2 FROM test t0_ WHERE t0_.id = ?, params: [2]