Customer Collection – How to Get Visitor Log

collection;

I'm looking to load the log/customer collection and am running into some issues.

When using Mage::getModel('log/customer')->load(1); the appropriate data is returned but if I try to load the collection, it just returns false.

Here is what was tried:

Mage::getModel('log/customer')->getCollection(); //returns false
Mage::getResourceModel('log/customer_collection'); //returns false

Any suggestions

Best Answer

Mage_Log is the module whih stores log details in Magento. It mainly logs following details

Customers who logged in 

Past Vistors

Vistors who currently active

Quotes etc.

If you dig more deeply, you can see that, you can get log-visitor collection. However log-customer collection is not available. It is logical since, most of the time, you will only query about logs of a specific customer, not of multiple customers at a time. I think this is why core team of magento didn't include collection for log-customers.

To Get Collection Of Customers

Now take a look on the Mage_Log module's model section. You can see the entity visitor only holds the collection. But through this collection we can access log-customer collection!. What ? I can get log-collection through visitors ? Yes. Because let us have a look on log_customer table.

+--------+------------+-------------+---------------------+---------------------+----------+
| log_id | `visitor_id` | customer_id | login_at            | logout_at           | store_id |
+--------+------------+-------------+---------------------+---------------------+----------+
|      1 |        105 |           2 | 2014-02-05 18:49:10 | 2014-02-05 13:19:10 |        4 |
|      2 |        105 |           2 | 2014-02-05 13:19:30 | NULL                |        4 |
|      3 |        113 |           3 | 2014-08-05 10:26:08 | NULL                |        1 |
|      4 |        325 |           3 | 2014-09-04 01:32:18 | NULL                |        1 |
+--------+------------+-------------+---------------------+---------------------+----------+

Did you see visitor_id that is used in the table. This means, every time a user visit, magento will generate a vistor_id for that user and store in the table log_visitor. When a registered user then login to his account using his credentials, log_customer table will get updated. So each time a customer visit, a unique visitor_id reference will be associated with that user. So due to this, using visitor collection , we can access log-customer collection.

For an exmaple a simple query is shown below.

$collection = Mage::getModel('log/visitor')->getCollection()->addFieldToFilter('store_id', array('eq' => 1));
$collection->getSelect()
            ->join( 
                array('log_customer'=> $collection->getTable('log/customer')),
                'main_table.`visitor_id`= log_customer.`visitor_id`'
            );
foreach ($collection->getItems() as  $item) {
    print_r($item->getData());
}

This collection filter will return every log-customer who are available for store id = 1. Is that what you need ? :)

Wrap up

So in short, you don't need any collection defined for log-customer entity. It is intended to access details of one customer at a time. If you need collection, you can use visitor collection for that. That is the correct way to do that :)

Hope that make sense