【发布时间】:2014-04-18 16:43:35
【问题描述】:
在开发环境中运行 Symfony 应用程序时,Web 调试工具栏允许我查看 Doctrine 生成了多少查询。控制台命令是否有类似的分析器选项?
【问题讨论】:
标签: symfony
在开发环境中运行 Symfony 应用程序时,Web 调试工具栏允许我查看 Doctrine 生成了多少查询。控制台命令是否有类似的分析器选项?
【问题讨论】:
标签: symfony
首先,symfony profiler 依赖于 Request。这就是为什么它不能在开箱即用的控制台命令中使用,并且可能不会被修复。 Related symfony issue
但您仍然可以访问默认的 DBAL 分析记录器。它应该是 Doctrine\DBAL\Logging\DebugStack 的实例 它具有公共查询属性,其中包含所有已执行的查询、参数、执行时间等。 每当您需要调试实际查询时 - 您都可以这样做
/** @var $em Registry */
$em = $this->getContainer()->get('doctrine')->getManager();
$profiler = $this->getContainer()->get('doctrine.dbal.logger.profiling.default');
$shop = $em->getRepository(Shop::class)->find(7);
$sku = $em->getRepository(Sku::class)->find(45);
// clear profiles data, we want to profile Criteria below
$profiler->queries = [];
$shopWares = $shop->getShopWarePagesForSku($sku);
$output->writeln(var_export($profiler->queries));
它会产生类似的输出
array (
3 =>
array (
'sql' => 'SELECT ...... FROM ShopWarePage t0 WHERE (t0.sku_id = ? AND t0.sku_id = ?)',
'params' =>
array (
0 => 45,
1 => 7,
),
'types' =>
array (
0 => 'integer',
1 => 'integer',
),
'executionMS' => 0.00075292587280273438,
),
)
【讨论】:
是的,--verbose 就像@manolo 提到的那样很有用。您可以从独白处理程序配置中控制 -v -vv -vvv 中的输出内容
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
bubble: false
verbosity_levels:
VERBOSITY_VERBOSE: INFO
VERBOSITY_VERY_VERBOSE: DEBUG
channels: ["!doctrine"]
console_very_verbose:
type: console
bubble: false
verbosity_levels:
VERBOSITY_VERBOSE: NOTICE
VERBOSITY_VERY_VERBOSE: NOTICE
VERBOSITY_DEBUG: DEBUG
channels: ["doctrine"]
请注意,您甚至可以禁用频道 -v 或 --verbose 将只输出指定详细级别的非原则日志。
【讨论】:
如文档中所述,分析器仅收集所有请求的信息。我相信控制台命令没有收集器。更深入地了解 Doctrine 执行的查询的方法之一是检查您的日志文件。例如,您可以在基于 Unix 的系统上执行以下操作:
tail -f app/logs/dev.log | grep doctrine
另见:http://symfony.com/doc/current/book/internals.html#profiler
【讨论】: