2017-01-23 60 views
1

我正在嘗試將自定義表加入到客戶集合中。 自定義表:Magento JoinLeft

+---------+--------------------+ 
| user_id | linked_customer_id | 
+---------+--------------------+ 
|  4 |     12 | 
+---------+--------------------+ 

我想將USER_ID添加到每個地方的customer_id匹配linked_customer_id項目。

我必須得下,但我收到:

柱未發現:1054未知列「main_table.entity_id」

$collection = Mage::getResourceModel('customer/customer_collection') 
     ->addNameToSelect() 
     ->addAttributeToSelect('entity_id') 
     ->addAttributeToSelect('email') 
     ->addAttributeToSelect('brand') 
     ->addAttributeToSelect('group_id') 
     ->joinAttribute('shipping_company', 'customer_address/company', 'default_shipping', null, 'left') 
     ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left') 
     ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left') 
     ->joinAttribute('shipping_telephone', 'customer_address/telephone', 'default_shipping', null, 'left') 
     ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left') 
     ->joinAttribute('shipping_country_id', 'customer_address/country_id', 'default_shipping', null, 'left') 
     ->joinAttribute('billing_vat_id', 'customer_address/vat_id', 'default_billing', null, 'left'); 

    $collection->getSelect()->joinLeft(
     array('salesrep' => 'custom_column'), 'main_table.entity_id=salesrep.user_id', 
     array('user_id' => 'salesrep.linked_customer_id') 
    ); 

    $this->setCollection($collection); 

回答

0

我可能是錯的,但我認爲,因爲customer/customer是EAV模型,而不是一個平面模型,它使用表的別名是e而非main_table

$collection->getSelect()->joinLeft(
    array('salesrep' => 'custom_column'), 
    'e.entity_id = salesrep.user_id', 
    array('user_id' => 'salesrep.linked_customer_id') 
); 

你會能夠通過調試select語句找到,例如:

echo (string) $collection->getSelect(); 
+0

你是完全正確的。情況就是如此。表別名的確是'e'。謝謝你明確的答案。 –