2010-10-26 19 views
1

我正在嘗試管理一個Adminhtml網格,該網格將一個我稱之爲「facility」的表加入到「customer_entity」及其所有屬性中。加入一個平臺到Magento的EAV表?

我注意到,因爲我的表是扁平的,並且模型繼承了Mage_Core_Model_Mysql4_Collection_Abstract,所以我無法使用我在後端看到的所有class_methods作爲示例。

我已經成功地能到我的表連接到customer_entity表執行以下操作:

$collection = Mage::getModel('facility/facility')->getCollection() 
       ->join('customer/entity', 'customer_id = entity_id'); 

不過,我真正需要的是隻是爲了讓客戶名稱。任何幫助?

回答

4

客戶姓名不存儲在一個字段中,它們是第一個,中間和最後一個名字的組合;並根據設置,前綴和後綴。客戶類有爲您組合這些的方法。

$collection = Mage::getModel('customer/customer')->getCollection() 
       ->addNameToSelect() 
       ->joinTable('facility/facility', 'entity_id=customer_id', array('*')); 

joinTable方法是EAV集合的一部分,這就是爲什麼你不會看到它在覈心MySQL4基於集合。第一個參數不是一個文字表名,而是你的config.xml中描述的一個實體表。

+0

如果我使用這種方法,我得到了「項目(Mage_Customer_Model_Customer)具有相同的ID」xxx「已存在」錯誤。 – 2015-08-11 04:54:18

+0

@piavgh您的自定義表格包含客戶「xxx」的多個記錄。確保您加入的列索引爲唯一。 – clockworkgeek 2015-08-11 12:11:35

1

一般來說,我與previous answer只是一些更正語法同意

 

    $collection = Mage::getModel('customer/customer')->getCollection() 
        ->addNameToSelect() 
        ->joinTable('facility/facility', 'customer_id=entity_id', array('*'), null, 'right'); 

空部分是WHERE子句和「右」是的類型,加入

或者,也許更好的決定是:

 

    $collection = Mage::getModel("facility/facility")->getCollection()->getSelect()->join('customer_entity', 'entity_id=customer_id', array('*')); 

+0

在第二種方式中,如何獲取客戶姓名? – 2015-08-11 04:52:31