1

How does one retrieve a collection of visitors using the Log/Visitor model? I've tried using the following code....

<?php 
error_reporting(E_ALL);
ini_set('display_errors',TRUE);
$root = $_SERVER['DOCUMENT_ROOT'];
require_once $root.'/app/Mage.php';
Mage::app('default');

$visitors = Mage::getModel('log/visitor')->getCollection()->load();
?>

But it returns an error, an excerpt from which is...

SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty

The query doesn't throw any errors until I add the 'load()' method to the chain. My question is similar to magento visitor logs, but that code example was missing the load() and the only answer resorted to using the resource model directly, which I don't think should be necessary.

Update:

Magento version being used is 1.4.1.1. Full exception trace:

Fatal error: Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty' in /home/dev_fluid/public_html/lib/Zend/Db/Statement/Pdo.php:234 Stack trace: #0 /home/dev_fluid/public_html/lib/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array) #1 /home/dev_fluid/public_html/lib/Zend/Db/Adapter/Abstract.php(468): Zend_Db_Statement->execute(Array) #2 /home/dev_fluid/public_html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('', Array) #3 /home/dev_fluid/public_html/lib/Varien/Db/Adapter/Pdo/Mysql.php(333): Zend_Db_Adapter_Pdo_Abstract->query('', Array) #4 /home/dev_fluid/public_html/lib/Zend/Db/Adapter/Abstract.php(706): Varien_Db_Adapter_Pdo_Mysql->query(Object(Varien_Db_Select), Array) #5 /home/dev_fluid/public_html/lib/Varien/Data/Collection/Db.php(707): Zend_Db_Adapter_Abstract->fetchAll(Object(Varien_Db_Select), Array) #6 /home/dev_fluid/public_html/lib/Varien/Data/Collection/Db.php(620): Varien_Data_Collect in /home/dev_fluid/public_html/lib/Zend/Db/Statement/Pdo.php on line 234

New trace, using's Jurgen's getTraceAsString() approach:

#0 /home/dev_fluid/public_html/lib/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array) 
#1 /home/dev_fluid/public_html/lib/Zend/Db/Adapter/Abstract.php(468): Zend_Db_Statement->execute(Array) 
#2 /home/dev_fluid/public_html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('', Array) 
#3 /home/dev_fluid/public_html/lib/Varien/Db/Adapter/Pdo/Mysql.php(333): Zend_Db_Adapter_Pdo_Abstract->query('', Array) 
#4 /home/dev_fluid/public_html/lib/Zend/Db/Adapter/Abstract.php(706): Varien_Db_Adapter_Pdo_Mysql->query(Object(Varien_Db_Select), Array) 
#5 /home/dev_fluid/public_html/lib/Varien/Data/Collection/Db.php(707): Zend_Db_Adapter_Abstract->fetchAll(Object(Varien_Db_Select), Array) 
#6 /home/dev_fluid/public_html/lib/Varien/Data/Collection/Db.php(620): Varien_Data_Collection_Db->_fetchAll(Object(Varien_Db_Select)) 
#7 /home/dev_fluid/public_html/lib/Varien/Data/Collection/Db.php(590): Varien_Data_Collection_Db->getData() 
#8 /home/dev_fluid/public_html/app/code/core/Mage/Log/Model/Mysql4/Visitor/Collection.php(300): Varien_Data_Collection_Db->load(false, false) 
#9 /home/dev_fluid/public_html/andy/visitor.php(11): Mage_Log_Model_Mysql4_Visitor_Collection->load() 
#10 {main}
4

1 回答 1

0

请将您的代码更改为:

<?php 
error_reporting(E_ALL);
ini_set('display_errors',TRUE);
$root = $_SERVER['DOCUMENT_ROOT'];
require_once $root.'/app/Mage.php';
Mage::app('default');

$visitors = Mage::getModel('log/visitor')->getCollection();
try {
    $x = $visitors->load();
    die('no exception');
}
catch (Exception $e) {
    var_dump($e->getTraceAsString());
}

如果异常发生在load()方法中,这应该会给你一个完整的跟踪。

如果该跟踪不能帮助您确定问题,我建议使用 PHP 调试器,如 xdebug、Zend Debug 或 DBG 等(取决于您的 IDE 支持的调试器)。

在方法之前设置断点load()并单步执行代码应该会给你一个线索,为什么查询是空的。

于 2012-07-15T09:48:15.553 回答