如何调试MySQL/Doctrine2查询?

Jie*_*eng 24 php mysql zend-framework doctrine-orm

我正在使用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();
Run Code Online (Sandbox Code Playgroud)

$progress 被宣布

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

在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$$
Run Code Online (Sandbox Code Playgroud)

beb*_*lei 84

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

$em->getConnection()
  ->getConfiguration()
  ->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger())
;
Run Code Online (Sandbox Code Playgroud)

  • 这种方式对我没用,我不得不这样称它:$ em-> getConnection() - > getConfiguration() - > setSQLLogger(new Doctrine\DBAL\Logging\EchoSQLLogger()); (14认同)

Row*_*ego 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);
Run Code Online (Sandbox Code Playgroud)

请注意,我们从$ 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);
Run Code Online (Sandbox Code Playgroud)


ajr*_*eal 6

如何使用mysql通用查询日志

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


amr*_*ree 5

使用Doctrine2 profiler + Firebug

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