如何在 zend 框架中打印精确的 sql 查询?
我有以下一段从模型中获取的代码,
...
$select = $this->_db->select()
->from($this->_name)
->where('shipping=?',$type)
->where('customer_id=?',$userid);
echo $select; exit; // which gives exact mysql query.
.....
当我在 zend 中使用更新查询时,
$up_value = array('billing'=> '0');
$this->update($up_value,'customer_id ='.$userid.' and address_id <> '.$data['address_Id']);
我想知道确切的 mysql 查询。有没有可能的方法在 zend 中打印 mysql 查询?善意的建议
I have the following piece of code which i taken from model,
...
$select = $this->_db->select()
->from($this->_name)
->where('shipping=?',$type)
->where('customer_id=?',$userid);
echo $select; exit; // which gives exact mysql query.
.....
When i use update query in zend like ,
$up_value = array('billing'=> '0');
$this->update($up_value,'customer_id ='.$userid.' and address_id <> '.$data['address_Id']);
Here i want to know the exact mysql query. Is there any possible way to print the mysql query in zend ? kindly advice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
选择对象在 Zend Framework 中具有 __toString() 方法。
来自 Zend Framework 手册:
另一种解决方案是使用 Zend_Db_Profiler。
即
http://framework.zend.com/manual/en/zend.db.select.html
Select objects have a __toString() method in Zend Framework.
From the Zend Framework manual:
An alternative solution would be to use the Zend_Db_Profiler.
i.e.
http://framework.zend.com/manual/en/zend.db.select.html
来自>= 2.1.4
from >= 2.1.4
我已经浏览了数百页,用谷歌搜索了很多,但没有找到任何确切的解决方案。
最后这对我有用。无论您处于控制器或模型中的哪个位置。这段代码在任何地方都对我有用。只要使用这个
最后这个东西对我有用。
I have traversed hundred of pages, googled a lot but i have not found any exact solution.
Finally this worked for me. Irrespective where you are in either controller or model. This code worked for me every where. Just use this
Finally this thing worked for me.
您可以使用
Zend_Debug::Dump($select->assemble());
来获取 SQL 查询。或者您可以启用 Zend DB FirePHP 分析器 它将以简洁的格式为您提供 Firebug 中的所有查询(甚至是 UPDATE 语句)。
编辑:
使用 FirePHP 进行分析也适用于 FF6.0+(不仅适用于链接中建议的 FF3.0)
You can use
Zend_Debug::Dump($select->assemble());
to get the SQL query.Or you can enable Zend DB FirePHP profiler which will get you all queries in a neat format in Firebug (even UPDATE statements).
EDIT:
Profiling with FirePHP also works also in FF6.0+ (not only in FF3.0 as suggested in link)
现在在 Zend2 上:
显示生成的 SQL来自 ZendDbSql 对象
Now on Zend2:
Displaying the generated SQL from ZendDbSql object
你可以打印..
you can print..
例子:
Example:
甚至更短:
和更更短:
even shorter:
and more shorter:
这个来自 Zend Framework 文档(即更新):(
奖励)我在我自己的模型文件中使用这个:
祝你有美好的一天:)
This one's from Zend Framework documentation (ie. UPDATE):
(Bonus) I use this one in my own model files:
Have a nice day :)
使用这个:-
或
Use this:-
or
查看 Zend_Db_Profiler。这允许您在准备和执行任何 SQL 语句时记录该语句。它适用于 UPDATE 语句以及 SELECT 查询。
Check out the Zend_Db_Profiler. This allows you to log any SQL statement as it is prepared and executed. It works for UPDATE statements as well as SELECT queries.
我用这种方式做到了
I have done this by this way
从探查器或查询对象返回的查询将包含占位符(如果您使用这些占位符)。
要查看 mysql 运行的确切查询,您可以使用常规查询日志。
这将列出自启用以来已运行的所有查询。
收集样本后,请不要忘记禁用此功能。
在活动服务器上;该日志可能会很快填满。
从 mysql 终端或查询工具(例如 MySQL Workbench)运行:
然后运行查询。
结果存储在“mysql.general_log”表中。
禁用查询日志:
验证它已关闭:
这帮助我找到占位符未被 zend db 替换的查询。用探查器看不到这一点。
The query returned from the profiler or query object will have placeholders if you're using those.
To see the exact query run by mysql you can use the general query log.
This will list all the queries which have run since it was enabled.
Don't forget to disable this once you've collected your sample.
On an active server; this log can fill up very fast.
From a mysql terminal or query tool like MySQL Workbench run:
then run your query.
The results are stored in the "mysql.general_log" table.
To disable the query log:
To verify it's turned off:
This helped me locate a query where the placeholder wasn't being replaced by zend db. Couldn't see that with the profiler.