php - 如何在运行时查看 DQL 查询准备好的查询?

标签 php doctrine doctrine-orm dql

function get_event($id){
            $query = $this->em->createQuery('SELECT e.name,e.date, e.time, e.venue, e.venueaddress,e.parish,e.genre, e.entryprice, e.phone, e.specialguests,
                                            e.weblink, e.otherinfo, e.flyer1, e.flyer2 from Events e WHERE e.id = :id');
            $query->setParameter('id', $id);


//CAN I VIEW THE QUERY AT THIS TIME?


            $result = $query->getResult();    
            return $result;    
        }

最佳答案

Haim Evgi 建议的 EchoSqlLogger,嗯,回显日志输出,因此您应该在您的网站上看到它。

如果您只想查看 Doctrine 会生成什么 SQL 查询,请使用:

$query = $this->em->createQuery('SELECT e.name,e.date, e.time, e.venue, e.venueaddress,e.parish,e.genre, e.entryprice, e.phone, e.specialguests,
                                        e.weblink, e.otherinfo, e.flyer1, e.flyer2 from Events e WHERE e.id = :id');

print $query->getSQL();

但请注意,参数不包含在该 SQL 字符串中,它们显示为占位符 (= ?)。

为了观察 Doctrine 的作用,我最常用的技术是启用 mysql(或您使用的任何数据库)query log (不要在负载较重的生产服务器上执行此操作!)。

如果查询日志位于/var/log/mysql/query.log下,我只需这样做:

# tail -f /var/log/mysql/query.log

(有关更多详细信息,请参阅 tail command)

并重新加载执行查询的页面。

关于php - 如何在运行时查看 DQL 查询准备好的查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7329288/

相关文章:

使用 varchar 数据类型作为输入的 PHP GET 请求

php - fatal error 异常 : Error: Call to a member function has() on a non-object

Doctrine 2 : how to clone all values from one object onto another except ID?

symfony - 原则 2 - 多对多关系 - 空集合

authentication - 基于接受的eula的Symfony2自定义用户检查器

php - 如何修复随机 php 代码被添加到服务器中的每个 php 文件上

php - 我可以使用 substr() 作为 in_array() 中的指针吗?

php - 使用正则表达式验证 CSV 中的项目

sql - Doctrine 更新查询失败

php - 覆盖持久性 :loadAll in EntityRepository