27

我正在使用带有 Zend Framework 和 Doctrine 2 的 MySQL。我认为即使您不使用 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$$
4

8 回答 8

93

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

$em->getConnection()
  ->getConfiguration()
  ->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger())
;
于 2011-01-03T14:51:05.490 回答
13

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

对于基本记录器,您必须将 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 框架中,

你来做这件事:

$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);
于 2011-11-14T20:43:35.613 回答
6

使用mysql通用查询日志怎么样?

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

于 2010-12-31T15:03:18.310 回答
5

使用 Doctrine2 分析器 + Firebug

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

于 2011-01-01T06:53:31.430 回答
2

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

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

于 2010-12-31T14:53:52.737 回答
2

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

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

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

于 2011-12-01T16:50:14.273 回答
2

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

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

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

在此处输入图像描述

于 2014-05-06T17:55:44.843 回答
1

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

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

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

请参阅此链接:Zend 启用 SQL 查询日志记录

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

于 2014-05-06T18:01:42.357 回答