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

发布于 2024-12-03 10:30:15 字数 514 浏览 0 评论 0原文

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;    
        }
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;    
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回梦 2024-12-10 10:30:15

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(或您使用的任何数据库)查询日志(不要在负载较重的生产服务器上执行此操作!)。

如果查询日志位于 /var/log/mysql/query.log 下,我只需执行以下操作:(

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

请参阅 tail 命令 了解更多详细信息)

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

The EchoSqlLogger as suggested by Haim Evgi, well, echo´s the log output, so you should see it on your website.

If you just want to see what SQL query Doctrine would generate, use:

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

But be aware, parameters are not included in that sql string, they are shown as placeholders (= ?).

The most common technique I use in order to watch what Doctrine does is enabling the mysql (or whatever db you use) query log (dont do this on a production server which is under heavy load!).

If the query log is under /var/log/mysql/query.log I just do this:

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

(see tail command for more details)

And reload the page which executes the query.

临走之时 2024-12-10 10:30:15

在教义 2.3.2 中

use Doctrine\DBAL\Logging\DebugStack;
$logger = new DebugStack();
$config->setSQLLogger($logger);

,现在你可以用 print_r($logger); 打印 $logger

In doctrine 2.3.2

use Doctrine\DBAL\Logging\DebugStack;
$logger = new DebugStack();
$config->setSQLLogger($logger);

now you can print $logger with print_r($logger);

初心未许 2024-12-10 10:30:15
//create query
$oQuery = $this->em->createQuery('SELECT tbl.id FROM mytable tbl WHERE tbl.id = :id');

//print query and params
echo "SQL: ".$oQuery->getSQL();
while($param = $oQuery->getParameters()->current()){
  echo "Param: ".$param->getName()." | ".$param->getValue()." | ".$param->getType();
  $oQuery->getParameters()->next();
}

SQL: SELECT tbl.id AS id0 FROM mytable tbl WHERE tbl.id = ?

参数: id | 1 |整数

//create query
$oQuery = $this->em->createQuery('SELECT tbl.id FROM mytable tbl WHERE tbl.id = :id');

//print query and params
echo "SQL: ".$oQuery->getSQL();
while($param = $oQuery->getParameters()->current()){
  echo "Param: ".$param->getName()." | ".$param->getValue()." | ".$param->getType();
  $oQuery->getParameters()->next();
}

SQL: SELECT tbl.id AS id0 FROM mytable tbl WHERE tbl.id = ?

Param: id | 1 | integer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文