如何记录集合的SQL查询?
-
16-10-2019 - |
题
我想为自定义集合记录SQL查询。我尝试了以下代码。但没有工作。任何建议将不胜感激。
Mage::log($collection->getSelect(),null,'test.log',true);
解决方案
如果您希望从集合中查询SQL查询,则需要将其施放为字符串。只是添加 (string)
到您先前编写的记录代码。
Mage::log((string)$collection->getSelect(),null,'test.log',true);
其他提示
试用 $Collection->printLogQuery(true);
这将打印收集查询。
正如Flyingmana向我解释的那样,感谢他。
$collection->load($printQuery = false, $logQuery = false)
加载后记录或打印查询很重要,因为在 beforeLoad
方法可以做很多事情。因此,第一个答案:
Mage::log((string)$collection->getSelect(),null,'test.log',true);
应在 load
叫做。
步骤1:
$result_colletion = print_r($collection->getSelect());
Mage::log($$result_colletion, null, custom_collection.log,true);
第2步: 在该登录到Magento Admin部分之后,并启用日志设置。请参阅下面。
系统>配置>开发人员>日志设置
步骤3: 之后,请参见var/ log/文件夹中的日志文件“ custom_collection.log”。
Mage::log($collection->getSelect()->__toString(), Zend_Log::DEBUG, 'test.log', true);
编辑:
而不是使用 null
(后备到 DEBUG
)作为第二个参数更好地使用其中之一 Zend_Log
:
const EMERG = 0; // Emergency: system is unusable
const ALERT = 1; // Alert: action must be taken immediately
const CRIT = 2; // Critical: critical conditions
const ERR = 3; // Error: error conditions
const WARN = 4; // Warning: warning conditions
const NOTICE = 5; // Notice: normal but significant condition
const INFO = 6; // Informational: informational messages
const DEBUG = 7; // Debug: debug messages