Magento Grid collection联表查询添加字段

来源:互联网 发布:手机网络老是连接超时 编辑:程序博客网 时间:2024/06/05 02:30

下面这个是我在做对后端显示用户客户端登陆源显示时遇到的,是后端Grid列表中的:
我的系统文件中自己建立的文件中Fun->Token->Model->Observer.php 可以通过文件中的generteToken方法得到相关的手机接口请求头信息,包括:Customer,device_id,Language,Platform等信息。

$old_token = $observer->getEvent()->getToken();$old_customer_id = Mage::helper('token')->getCustomerId($old_token);

$old_token得到的是用户的ID的加密信息,而$old_customer_id可以将加密的用户ID解密

$mobileInfo = Mage::getModel('fun_customer/mobileinfo')->load($device_id, 'device_id');$customer_device = Mage::getModel('fun_customer/mobileinfo')->load($customer->getId(), 'customer_id');

$mobileInfo是通过数据库中字段device_id,对应的$device_id的值来查询对应行的数据。 $customer_device是通过数据库中字段customer_id,对应的$customer->getId()的值来查询对应行的数据。

联表查询相关样例及解析:

$collection = Mage::getResourceModel('customer/customer_collection');$collection -> getSelect()-> joinLeft(array("info"=>'customer_mobile_info'),    'e.entity_id = info.customer_id',array( "info.platform"));

所打印出来的数据库语句是:SELECT e.*, info.platform FROM customer_entity AS e LEFT JOIN customer_mobile_info AS info ON e.entity_id = info.customer_id WHERE (e.entity_type_id = ‘1’) 意思是联表查询将customer_mobile_info 中的platform字段添加到customer_entity数据表中,并将platform的数据也联表到customer_mobile_info 中

而在Grid中添加这个字段的渲染器,代码如下:

$this->addColumn('platform', array(             'header'=> Mage::helper('customer')->__('Platform'),             'width' => '80px',             'type'  => 'text',             'index' => 'platform',             'filter_index'=>'info.platform',       ));