1
votes

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}
1
Your code to retrieve a visitor collection should work without problems. Posting the exception trace and the Magento version used may be helpful here.Jürgen Thelen
The expection trace you posted is incomplete; the whole bootstrap is missing ({main}, etc).Jürgen Thelen
Hi Jurgen - Forgive my ignorance, but how do a generate the relevant debug info? I've currently got display errors on, developer mode on and errors/local.xml file set up.Yorkshire Bear
Usually what you have enabled is enough to get exception traces. It looks like you copy/pasted it incompletely or it got cut off somehow. The last three lines of a Magento trace usually contain Mage.php, index.php and {main}. Yours doesn't. So we cannot follow the process flow the issue takes. Have a look at e.g. http://stackoverflow.com/q/10907649/693207 to see how a full trace usually looks like.Jürgen Thelen
Hi @Jürgen Thelen - I don't know why the trace I'm getting isn't in the usual format. I even tried catching the error and using echo Varien_Debug::backtrace(true, true); but just blank. I think I might have to give up?!Yorkshire Bear

1 Answers

0
votes

Please change your code to:

<?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());
}

If the exception occurs within the load() method, this should give you a full trace.

If that trace doesn't help you to pin down the issue, I'd recommend to use a PHP debugger like xdebug, or Zend Debug, or DBG, etc. (depending on what debugger your IDE supports).

Setting a breakpoint right before the load() method and single stepping thru the code should show give you a clue, why the query is empty.