如何从eav_attribute表获取实体(例如客户)的数据以显示在“客户网格"中以进行管理 [英] How to get data for an entity (for example customer) from eav_attribute table to be shown in Customer Grid for admin

查看:81
本文介绍了如何从eav_attribute表获取实体(例如客户)的数据以显示在“客户网格"中以进行管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经扩展了Magento的客户信息表格,以便为客户存储其他属性.让我们称之为"customer_referrer_id".

I have extended Magento’s customer information form to store an additional attribute for customer. Lets call it ‘customer_referrer_id’.

我有一个推荐人"角色,他只能访问客户网格和订单网格.但是,我想限制引荐来源网址,以仅查看网格中已将customer_referrer_id设置为已登录的引荐来源地址ID的那些客户.类似地,对于订单,已登录的引荐来源网址应仅能查看那些由拥有customer_referrer_id = loginin_referrer_id.

I have a role ‘referrer ‘ who has access to customer grid and order grid only. But, I want to restrict a referrer to see only those customers in the grid who have the customer_referrer_id set as the referrer’s id who has logged in. Similarly for orders, the logged in referrer shall be able to see only those orders made by customers who have customer_referrer_id = loggedin_referrer_id.

我已经知道如何覆盖模块,并且必须主要覆盖Adminhtml/Block/Customer/Grid :: _ prepareCollection和Adminhtml/Block/Sales/Order/Grid :: _ prepareCollection

I already know how to override a module and that I have to mainly override Adminhtml/Block/Customer/Grid::_prepareCollection and Adminhtml/Block/Sales/Order/Grid::_prepareCollection

我正在使用Magento 1.4.1.1

I am using Magento 1.4.1.1

这是我在app/etc/modules/Myproject_Adminhtml中的模块声明文件

This is my module declaration file in app/etc/modules/Myproject_Adminhtml

<?xml version="1.0"?>

<config>
    <modules>
        <Myproject_Adminhtml>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Sales />
            </depends>
        </Myproject_Adminhtml>
    </modules>
</config>

本地/Myproject/Adminhtml/etc/中的模块config.xml如下:

And my modules config.xml in local/Myproject/Adminhtml/etc/ is as follows:

<config>
    <modules>
        <Myproject_Adminhtml>
            <version>1.0.0</version>
        </Myproject_Adminhtml>    
    </modules>

    <global>
          <blocks>
            <adminhtml>
                <rewrite>
                <sales_order_grid>Myproject_Adminhtml_Block_Sales_Order_Grid</sales_order_grid>
                <customer_grid>Myproject_Adminhtml_Block_Customer_Grid</customer_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

还有

class Myproject_Adminhtml_Block_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('created_at')
        ->addAttributeToSelect('group_id')
        ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
        ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
        ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
        ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
        ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');    

        $this->setCollection($collection);

        $referrer_id = Mage::getSingleton('admin/session')->getUser()->getId();
        Mage::log('Logged in admin has id: ' . $referrer_id);

        return parent::_prepareCollection();
    }  
}

推荐答案

我的第一次尝试是(对于提到的两个文件),

My first attempt would be (for both files mentioned),

$collection->addAttributeToFilter('customer_referrer_id', $referrer_id);

$referrer_id是您必须从登录用户中检索的值.由于您似乎正在使用管理员用户(即与客户分离的实体),因此这是一种检索方式;

Where $referrer_id is the value you must retrieve from the logged in user. Since you appear to be using admin users - which are separate entities from customers - this is one way of retrieving;

$referrer_id = Mage::getSingleton('admin/session')->getUser()->getId();

请注意,仅从数据库中看不出当前登录的管理员用户,因此它不能成为表联接.

Note the currently logged in admin user is not apparent from the database alone so it cannot be a table join.

另一方面,我会将前端用于引荐来源网址,而不是使用admin.我会将新客户及其订单显示在推荐人的客户帐户中,类似于他们自己的我的订单"页面.当然,我不知道您还必须满足哪些其他要求.

On another point I would use the frontend for referrers instead of the admin. I would have the new customers and their orders shown in the referrer's customer account, similar to their own "My Orders" page. Of course I don't know what other requirements you have to attend to.

第二部分

Second part

覆盖Mage_Adminhtml_Block_Sales_Order_Grid::_prepareCollection()看起来像这样:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('customer/customer_collection');
    $collection->getSelect()->reset('columns'); // remove all customer columns
    $collection->addAttributeToFilter('entity_id', $referrer_id);
    $collection->joinTable('sales/order_grid', 'customer_id=entity_id', array('*'));

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

这是必需的,因为原始表sales/order_grid是平面的,不是实体集合,因此不能与属性联接.上面的步骤是相反的,首先是从客户集合开始,然后再加入平板.

This is necessary because the original table sales/order_grid is flat, not an entity collection, and so cannot be joined with attributes. The above works in reverse, by starting with a customer collection and then joining the flat table after.

一种可能更整洁的方法是用您自己的执行所有操作的集合类覆盖sales/order_grid_collection.尽管它更好地遵循了编码约定,但最终工作量更大,功能也不再更多.

A possibly neater method would be to override sales/order_grid_collection with your own collection class which performs all this. Although it better follows coding conventions it is more work and no more functional in the end.

这篇关于如何从eav_attribute表获取实体(例如客户)的数据以显示在“客户网格"中以进行管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆