Skip to main navigation Skip to main content Skip to page footer

Extbase Repository: Debug full SQL query

How can I debug a full SQL query in an Extbase Repository function to find out what is requested against the database?

It seems hard to output a full SQL query in an Extbase Repository; but it is not. Use a simple function to debug a query:

/**
 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
 * @return void
 */
private function debugQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query): void
{
	$typo3DbQueryParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class);
	$queryBuilder = $typo3DbQueryParser->convertQueryToDoctrineQueryBuilder($query);
	$sql = $queryBuilder->getSQL();
	$parameters = $queryBuilder->getParameters();
	$fullSql = $sql;
	foreach ($parameters as $key => $value) {
		if (is_string($value)) {
			$value = "'" . $value . "'";
		}
		$fullSql = str_replace(':' . $key, $value, $fullSql);
	}
	\TYPO3\CMS\Core\Utility\DebugUtility::debug($fullSql,'SQL query:');
}

In your function with your special query it is possible then to send your query to the debug function like this:

$this->debugQuery($query);

It works also on a commandline script (CLI).