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

3 Answers 3

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

9
  • great answer, this code throws an error Item (Mage_Log_Model_Visitor) with the same id "xxx" already exist any suggestions
    – pzirkind
    Sep 5, 2014 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 Sep 5, 2014 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 5, 2014 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 Sep 5, 2014 at 13:25
  • 1
    lol spoken like a true coder ;) thanks again
    – pzirkind
    Sep 5, 2014 at 13:27
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.

1
  • true, what can be done to create one without changing the core
    – pzirkind
    Sep 4, 2014 at 22:50
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;
    }
    }
3
  • I dont think this will work, because the class you are are trying to rewrite... actually doesn't exist ! Sep 5, 2014 at 7:06
  • thanks for the idea, tried a variant of this, looking though for a cleaner solution
    – pzirkind
    Sep 5, 2014 at 13:15
  • that why programmer_rkt i have rewrite class and as i am not inherent it parent class on rewrite class so it not creating any issue
    – Amit Bera
    Sep 6, 2014 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.