【问题标题】:How to debug MySQL/Doctrine2 Queries?如何调试 MySQL/Doctrine2 查询?
【发布时间】:2011-06-02 00:28:29
【问题描述】:

我正在使用 MySQL 和 Zend Framework & Doctrine 2。我想即使你不使用 Doctrine 2,你也会熟悉类似的错误

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ASC' at line 1

问题是我没有看到完整的查询。如果没有 ORM 框架,我可能可以轻松地回显 sql,但是有了框架,我如何找出它试图执行的 SQL?我将错误缩小到

$progress = $task->getProgress();

$progress 被声明

// Application\Models\Task
/**
 * @OneToMany(targetEntity="TaskProgress", mappedBy="task")
 * @OrderBy({"seq" = "ASC"})
 */
protected $progress;

在 MySQL 中,任务类看起来像

CREATE TABLE `tasks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `owner_id` int(11) DEFAULT NULL,
  `assigned_id` int(11) DEFAULT NULL,
  `list_id` int(11) DEFAULT NULL,
  `name` varchar(60) NOT NULL,
  `seq` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `tasks_owner_id_idx` (`owner_id`),
  KEY `tasks_assigned_id_idx` (`assigned_id`),
  KEY `tasks_list_id_idx` (`list_id`),
  CONSTRAINT `tasks_ibfk_1` FOREIGN KEY (`owner_id`) REFERENCES `users` (`id`),
  CONSTRAINT `tasks_ibfk_2` FOREIGN KEY (`assigned_id`) REFERENCES `users` (`id`),
  CONSTRAINT `tasks_ibfk_3` FOREIGN KEY (`list_id`) REFERENCES `lists` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1$$

【问题讨论】:

  • 如果你有一个Doctrine查询对象,在你贴的代码中不清楚,你可以运行$query->getSqlQuery()查看SQL。在您的代码中找到您的查询对象并将其输出以查看发生了什么。或者,您可以在 SQL 中打开查询日志以查看它是什么。

标签: php mysql zend-framework doctrine-orm


【解决方案1】:

尝试在您和 MySQL 服务器之间使用 Mysql 代理 (http://forge.mysql.com/wiki/MySQL_Proxy)。然后你可以配置这个代理来记录所有的请求。

http://mysql.stu.edu.tw/tech-resources/articles/proxy-gettingstarted.html

【讨论】:

【解决方案2】:

mysql general query log怎么样?

一般查询日志是mysqld正在做什么的一般记录。当客户端连接或断开连接时,服务器将信息写入此日志,并记录从客户端接收到的每个 SQL 语句。当您怀疑客户端出现错误并想确切了解客户端发送到 mysqld 的内容时,通用查询日志非常有用。

【讨论】:

  • 我无法在 CLI 中使用 --general_log=1 --general_log_file="..." 启用它,但我设法在 MySQL 中使用 SET global general_log = 'ON' 启用它
【解决方案3】:

使用 Doctrine2 profiler + Firebug

https://github.com/mridgway/ZendX_Doctrine2/

【讨论】:

    【解决方案4】:

    Doctrine 2 中最简单的查询调试解决方案:

    $em->getConnection()
      ->getConfiguration()
      ->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger())
    ;
    

    【讨论】:

    • 这种方式对我不起作用,我不得不这样称呼它: $em->getConnection()->getConfiguration()->setSQLLogger( new Doctrine\DBAL\Logging\EchoSQLLogger()) ;
    • 这是迄今为止最好的答案。
    • 在大多数情况下没问题,但在某些情况下还不够......当你的参数有问题时...... mysql一般查询日志给出将使用参数执行的确切SQL查询替换。
    • 没有办法通过 YAML 配置进行设置,是吗?看起来像DoctrineBundle does not supports anything other than logging。 – 我尝试将默认记录器标记为 services.yaml,使用Doctrine\DBAL\Logging\EchoSQLLogger: '@doctrine.dbal.logger',但似乎没有任何效果。
    • 我反其道而行之——在services.yaml 中添加doctrine.dbal.logger(newline)class: Doctrine\DBAL\Logging\EchoSQLLogger 使其工作!
    【解决方案5】:

    您必须使用 DBAL SQLLogger。您可以使用基本的原生 SQL Logger \Doctrine\DBAL\Logging\EchoSQLLogger,也可以使用 Doctrine\DBAL\Logging\SQLLogger 的接口来实现。 p>

    对于基本记录器,您必须将 SQL 记录器附加到 EntityManager。因此,在您的 Doctrine 引导文件中使用:

    $config = new Doctrine\ORM\Configuration ();
    // ... config stuff
    $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
    $connectionParams = array(
            'dbname' => 'example',
            'user' => 'example',
            'password' => 'example',
            'host' => 'localhost',
            'driver' => 'pdo_mysql');
    //make the connection through an Array of params ($connectionParams)
    $em = EntityManager::create($connectionParams, $config);
    

    请注意,我们从 $connectionParams 数组创建 EntityManager。

    重要:。如果您使用 DBAL 连接来创建 EntityManager,则必须将其附加到 DBAL 连接和 ORM EntityManager 中。例如,在 Zend Framework 中,

    你这样做:

    $config = new Doctrine\ORM\Configuration ();
    // ...config stuff
    // LOGGER
    $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
    
    // make the connection through DBAL (DriverManager::getConnection)
    // note that we attach $config in $connApp(DBAL) and $emApp(ORM)
    $connApp = DriverManager::getConnection($connectionParams, $config);
    $emApp = EntityManager::create($connApp, $config);
    Zend_Registry::set('emApp', $emApp);
    Zend_Registry::set('connApp', $connApp);
    

    【讨论】:

      【解决方案6】:

      我写了一篇关于这个主题的博客文章,其中包含一些在 Zend Framework 中设置分析 Doctrine 2 的说明

      ZFDebug 已经很不错了,而且有一个 Doctrine 2 插件

      http://labs.ultravioletdesign.co.uk/profiling-doctrine-2-with-zend-framework/

      【讨论】:

        【解决方案7】:

        如果您在 ZF2 中进行开发,您可以使用 beberlei 发布的上述解决方案,尽管这确实会在您的显示器上显示出来,这不是最佳实践,但很有用。

        $em->getConnection()
          ->getConfiguration()
          ->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger())
        ;
        

        或者您可以使用ZendDeveloperTools,它会在工具栏上显示执行查询。

        【讨论】:

          【解决方案8】:

          也许您应该在您的 [开发] 环境中使用 Zend_Db_Profile

          这将记录您的查询以及运行时和其他信息。

          如果您有记录异常的错误日志,那么所有内容都可以在您的日志文件中找到。

          查看此链接:Zend Enable SQL Query logging

          另请参阅此 Zend Framework Helper:https://github.com/jokkedk/ZFDebug

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-11-24
            • 2012-08-14
            • 1970-01-01
            • 1970-01-01
            • 2013-12-23
            • 1970-01-01
            相关资源
            最近更新 更多