【问题标题】:Count queries to database in Doctrine2在Doctrine2中计算对数据库的查询
【发布时间】:2011-01-23 11:34:03
【问题描述】:

如何在 Doctrine2 中获取对数据库的查询计数?我需要这个只是为了统计,并了解更多教义是如何工作的,在不同情况下生成了多少查询。但无论如何,如何做到这一点?

【问题讨论】:

    标签: doctrine-orm


    【解决方案1】:
    $stack = new \Doctrine\DBAL\Logging\DebugStack();
    $entityManager->getConfiguration()->setSQLLogger($stack);
    // do stuff
    var_dump($stack);
    

    【讨论】:

      【解决方案2】:

      只是添加到接受的答案。

      要从 Symfony 2.x 控制器的上下文中执行此操作:

      $doctrine = $this->get('doctrine');
      $doctrine = $this->getDoctrine();    
      $em = $doctrine->getConnection();
      
      // $doctrine->getManager() did not work for me 
      // (resulted in $stack->queries being empty array)
      
      $stack = new \Doctrine\DBAL\Logging\DebugStack();
      $em->getConfiguration()->setSQLLogger($stack);
      
      
      ... // do some queries
      
      var_dump($stack->queries);
      

      感谢这个帖子:http://vvv.tobiassjosten.net/symfony/logging-doctrine-queries-in-symfony2/

      【讨论】:

      • $doctrine->getManger() did not work for me 是不是因为缺少a
      猜你喜欢
      • 2018-03-03
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多