简体   繁体   English

如何调试MySQL / Doctrine2查询?

[英]How to debug MySQL/Doctrine2 Queries?

I am using MySQL with Zend Framework & Doctrine 2. I think even if you don't use Doctrine 2, you will be familiar with errors like 我正在使用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

The problem is that I don't see the full query. 问题是我没有看到完整的查询。 Without an ORM framework, I could probably echo the sql easily, but with a framework, how can I find out what SQL its trying to execute? 没有ORM框架,我可能很容易回应sql,但是使用框架,我怎样才能找到它试图执行的SQL? I narrowed the error down to 我把错误缩小到了

$progress = $task->getProgress();

$progress is declared 声明$progress

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

In MySQL, the task class looks like 在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$$

Most simple solution for debugging queries in Doctrine 2: 在Doctrine 2中调试查询的最简单的解决方案:

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

You have to use a DBAL SQLLogger. 您必须使用DBAL SQLLogger。 You can use the basic native SQL Logger \\Doctrine\\DBAL\\Logging\\EchoSQLLogger , or you can implement yours with an interface of Doctrine\\DBAL\\Logging\\SQLLogger . 您可以使用基本的本机SQL Logger \\ Doctrine \\ DBAL \\ Logging \\ EchoSQLLogger ,也可以使用Doctrine \\ DBAL \\ Logging \\ SQLLogger接口实现您的。

For the basic logger you must attach the SQL Logger to the EntityManager. 对于基本记录器,您必须将SQL Logger附加到EntityManager。 So, in your Doctrine bootstrap file use: 因此,在您的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);

Note that we create our EntityManager from the $connectionParams array. 请注意,我们从$ connectionParams数组创建EntityManager。

Important: . 重要的是: If you use a DBAL Connection for creating your EntityManager, you have to attach it in both, the DBAL Connection and the ORM EntityManager. 如果使用DBAL Connection创建EntityManager,则必须将其附加到DBAL Connection和ORM EntityManager中。 For example, in Zend Framework, 例如,在Zend Framework中,

You do this: 你做这个:

$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);

how about using mysql general query log ? 如何使用mysql通用查询日志

The general query log is a general record of what mysqld is doing. 通用查询日志是mysqld正在执行的操作的一般记录。 The server writes information to this log when clients connect or disconnect, and it logs each SQL statement received from clients. 当客户端连接或断开连接时,服务器会将信息写入此日志,并记录从客户端收到的每个SQL语句。 The general query log can be very useful when you suspect an error in a client and want to know exactly what the client sent to mysqld. 当您怀疑客户端中存在错误并想要确切知道客户端发送给mysqld的内容时,通用查询日志非常有用。

Use Doctrine2 profiler + Firebug 使用Doctrine2 profiler + Firebug

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

Try to use Mysql proxy between you and the MySQL server ( http://forge.mysql.com/wiki/MySQL_Proxy ). 尝试在您和MySQL服务器之间使用Mysql代理( http://forge.mysql.com/wiki/MySQL_Proxy )。 Then you can configure this proxy to log all requests. 然后,您可以配置此代理以记录所有请求。

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

I wrote a blog article on this topic with some instructions for setting up profiling Doctrine 2 in Zend Framework 我写了一篇关于这个主题的博客文章,其中有一些关于在Zend Framework中设置分析Doctrine 2的说明

ZFDebug is already very nice, and there is a Doctrine 2 plugin for it ZFDebug已经非常好了,并且有一个Doctrine 2插件

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

If your developing in ZF2 you can either use the solution above posted by beberlei , though this does echo it out to your display which is not exactly best practice but useful. 如果您在ZF2中进行开发,您可以使用beberlei发布的上述解决方案,尽管这确实反映在您的显示器上,这不是最佳实践但有用。

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

or you could use ZendDeveloperTools which displays the execute query on the toolbar. 或者您可以使用ZendDeveloperTools在工具栏上显示执行查询。

在此输入图像描述

Maybe you should use a Zend_Db_Profile in your [development] environment. 也许你应该在[开发]环境中使用Zend_Db_Profile

This will log your queries with runtime and other informations. 这将使用运行时和其他信息记录您的查询。

If you have an error log that logs your exceptions, then everything can be found in your log files. 如果您有记录异常的错误日志,那么可以在日志文件中找到所有内容。

See this link: Zend Enable SQL Query logging 请参阅此链接: Zend启用SQL查询日志记录

Also see this Zend Framework Helper: https://github.com/jokkedk/ZFDebug 另请参阅Zend Framework Helper: https//github.com/jokkedk/ZFDebug

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM