如何为 Shopware 6 中的存储库搜索的调试目的转储 SQL 查询?
How do dump the SQL query for debugging purpose of a repository search in Shopware 6?
如何转储 SQL 类似
的查询
$searchResult = $this->propertyGroupOptionRepository->search($criteria, $context);
我知道有调试栏,但这是在 PHPUnit 测试中,调试栏在那里不可用?
更新2:这也包含在FroshDevelopmentHelper
测试前类似这样的东西基本有效:
Kernel::getConnection()->getConfiguration()->setSQLLogger(new EchoSQLLogger());
这会将 SQL 查询打印到控制台。
缺点:
- 参数未内联到 SQL 查询中
- UUID 参数显示为二进制数据,难以复制和粘贴它们来调试查询
改进版本-使用核心代码格式化查询:
class EchoRunableSQLLogger implements \Doctrine\DBAL\Logging\SQLLogger
{
public function startQuery($sql, ?array $params = null, ?array $types = null)
{
$doctrineExtension = new \Shopware\Core\Profiling\Twig\DoctrineExtension();
echo $doctrineExtension->replaceQueryParameters(
$sql,
array_merge($params ?? array(), $types ?? array())
) . ';' . PHP_EOL;
}
public function stopQuery()
{
}
}
\Shopware\Core\Kernel::getConnection()->getConfiguration()->setSQLLogger(new EchoRunableSQLLogger());
如何转储 SQL 类似
的查询$searchResult = $this->propertyGroupOptionRepository->search($criteria, $context);
我知道有调试栏,但这是在 PHPUnit 测试中,调试栏在那里不可用?
更新2:这也包含在FroshDevelopmentHelper
测试前类似这样的东西基本有效:
Kernel::getConnection()->getConfiguration()->setSQLLogger(new EchoSQLLogger());
这会将 SQL 查询打印到控制台。
缺点:
- 参数未内联到 SQL 查询中
- UUID 参数显示为二进制数据,难以复制和粘贴它们来调试查询
改进版本-使用核心代码格式化查询:
class EchoRunableSQLLogger implements \Doctrine\DBAL\Logging\SQLLogger
{
public function startQuery($sql, ?array $params = null, ?array $types = null)
{
$doctrineExtension = new \Shopware\Core\Profiling\Twig\DoctrineExtension();
echo $doctrineExtension->replaceQueryParameters(
$sql,
array_merge($params ?? array(), $types ?? array())
) . ';' . PHP_EOL;
}
public function stopQuery()
{
}
}
\Shopware\Core\Kernel::getConnection()->getConfiguration()->setSQLLogger(new EchoRunableSQLLogger());