如何调试MySQL / Doctrine2查询?

时间:2010-12-31 14:29:16

标签: php mysql zend-framework doctrine-orm

我正在使用MySQL与Zend Framework&学说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$$

8 个答案:

答案 0 :(得分:81)

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

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

答案 1 :(得分:12)

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

对于基本记录器,您必须将SQL Logger附加到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 Connection创建EntityManager,则必须将其附加到DBAL Connection和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);

答案 2 :(得分:6)

如何使用mysql general query log

  

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

答案 3 :(得分:5)

使用Doctrine2 profiler + Firebug

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

答案 4 :(得分:2)

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

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

答案 5 :(得分:2)

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

ZFDebug已经非常好了,它有一个Doctrine 2插件

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

答案 6 :(得分:1)

如果您在ZF2中进行开发,您可以使用 beberlei 发布的上述解决方案,虽然这会将其反映到您的显示器上,这不是最佳做法,但很有用。

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

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

enter image description here

答案 7 :(得分:1)

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

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

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

请参阅此链接:Zend Enable SQL Query logging

另见Zend Framework Helper:https://github.com/jokkedk/ZFDebug