我是Magento的新手。我正在尝试添加显示在客户网格上的最后一个登录值。它返回一个空值。我已阅读其他教程,但它没有多大帮助。 Magento版本是1.7。这是我的代码:
$customer = Mage::getSingleton('customer/session')->getCustomer();
$logCustomer = Mage::getModel('log/customer')->load($customer ->getId());
$lastVisited = $logCustomer->getLoginAt();
$this->addColumn('$lastVisited', array(
'header' => Mage::helper('customer')->__('Last Login'),
'type' => 'datetime',
'align' => 'center',
'index' => '$lastVisited',
'gmtoffset' => true
));
答案 0 :(得分:1)
Magento将登录时间存储在下表中:
log_customer
但是,这些数据也会定期清理(参见:Mage_Log_Model_Resource_Log::_cleanCustomers
,这是通过Magento cron触发的。)
有多种方法可以处理您的任务。
1)非持久性 - 我只是想看到最近的数据(我可以忽略log_customer定期清理)
在这种情况下,您可以依赖log_customer
中的数据并将其显示在Manage Customers Grid中。
扩展Mage_Adminhtml_Block_Customer_Grid并在_prepareCollection中添加以下内容:
$collection->getSelect()->columns(array('last_login_at' => new Zend_Db_Expr ("(SELECT login_at
FROM `log_customer`
WHERE customer_id =e.entity_id
ORDER BY log_id DESC
LIMIT 1)")));
之前:$this->setCollection($collection);
注意:使用正确的Magento函数获取log_customer表名,我的查询只是例如
2)持久 - 我想总是看到数据
last_login_at
的客户实体添加新属性
(日期时间)。custom_login
事件添加观察者以更新此事件
属性。addColumn
函数来显示此新属性。@ user1414056
关于您的代码:
'$lastVisited'
相关的公平观点(这只是显示
缺乏PHP编程经验通过更好地理解Zend框架和OOP编程,您将能够实际工作并使用Magento完成工作。
答案 1 :(得分:-1)
你的'$ lastVisited'不起作用:在php变量中,只有当它们在双引号中时才会在字符串中计算。
编辑:
好吧,magento的列系统只有在链接到网格的集合中可用时才显示值。
您必须添加要在网格集中显示的日志信息。
例如,请查看Mage_Adminhtml_Block_Customer_Online_Grid::_prepareCollection()
完成后,您将添加以下列:
$this->addColumn('login_at', array(
'header' => Mage::helper('customer')->__('Last Login'),
'type' => 'datetime',
'align' => 'center',
'index' => 'login_at',
'gmtoffset' => true
));