首页 > 解决方案 > 如何在 Shopware 6 中转储 SQL 查询以用于调试存储库搜索?

问题描述

如何转储类似的 SQL 查询

$searchResult = $this->propertyGroupOptionRepository->search($criteria, $context);

我知道有一个调试栏,但这是在 PHPUnit 测试中,调试栏在那里不可用?

标签: phpdebuggingshopware

解决方案


更新 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());

推荐阅读