4

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

pzirkind
  • 2,906
  • 1
  • 30
  • 41

3 Answers3

6

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

Rajeev K Tomy
  • 17,234
  • 6
  • 61
  • 103
  • great answer, this code throws an error Item (Mage_Log_Model_Visitor) with the same id "xxx" already exist any suggestions – pzirkind Sep 05 '14 at 13:15
  • 1
    that is because you have more than one entry with same visitor_id in log_customer. The code works only when all visitor_id in log_customer table is unique – Rajeev K Tomy Sep 05 '14 at 13:18
  • hmmm, does that mean that this code will crash on production...in other words is there a way i can avoid duplicates from appearing, or am i looking at this wrong – pzirkind Sep 05 '14 at 13:20
  • 1
    no dont use this code in production environment directly. I just show an example code. My point is, you can use log-visitor to get collection of a customer. It is your job to do a good coding based on this point. :). I just want to answer your question and my answer fulfills what the question demands – Rajeev K Tomy Sep 05 '14 at 13:25
  • 1
    lol spoken like a true coder ;) thanks again – pzirkind Sep 05 '14 at 13:27
  • what can i do to get distinct on a join – pzirkind Sep 05 '14 at 13:29
  • lol. You know what this is the first time, I use getSelect() and join() in my code. So I dont think I can say whether that code is good. This is good code, if you modify little bit. Means you need to avoid duplicate entries. Actually I dont know how to do that. If you find it how can we do that, please inform me – Rajeev K Tomy Sep 05 '14 at 13:30
  • he he ... that is what I am thinking about now :) – Rajeev K Tomy Sep 05 '14 at 13:31
  • but good question. It teached me a lot. that is why +1 for this question – Rajeev K Tomy Sep 05 '14 at 13:32
3

This is because if you see the log Module in Magento there is no collection class for the customer model. There is no collection defined for you to instantiate.

Paras Sood
  • 2,540
  • 1
  • 14
  • 23
1

overwrite class Mage_Log_Model_Customer resource class of log

using

 <models>
  <youmodulemodellnname>
  <class>yourModuleNmaeSapce_Modulename_Modelname</class>
  </youmodulemodellnname>
     <log_resource>
        <rewrite>
            <customer_collection>yourModuleNmaeSapce_Modulename_Modelname_Resource_Custome_Collectionr</customer_collection>
        </rewrite>
    </log_resource>
  </log>
</models>

now create a collection file code is

<?php
class yourModuleNmaeSapce_Modulename_Modelname_Resource_Custome_Collectionr
extends Mage_Core_Model_Resource_Db_Collection_Abstract{
    protected function _constuct(){
        $this->_init('log/customer'); 
    $this->_visitorTable        = $this->getTable('log/visitor');
    $this->_visitorInfoTable    = $this->getTable('log/visitor_info');
    $this->_urlTable            = $this->getTable('log/url_table');
    $this->_urlInfoTable        = $this->getTable('log/url_info_table');
    $this->_customerTable       = $this->getTable('log/customer');
    $this->_quoteTable          = $this->getTable('log/quote_table');

    }


  public function function addLoadSelect()
    {
        $select =$this->getSelect;


            $table  = $this->getMainTable();
            $select
                ->joinInner(
                    array('lvt' => $this->_visitorTable),
                    "lvt.visitor_id = {$table}.visitor_id",
                    array('last_visit_at'))
                ->joinInner(
                    array('lvit' => $this->_visitorInfoTable),
                    'lvt.visitor_id = lvit.visitor_id',
                    array('http_referer', 'remote_addr'))
                ->joinInner(
                    array('luit' => $this->_urlInfoTable),
                    'luit.url_id = lvt.last_url_id',
                    array('url'))
                ->order("{$table}.login_at DESC");

        return $select;
    }
    }
Amit Bera
  • 77,456
  • 20
  • 123
  • 237