Magento 向左加入

Magento JoinLeft

我正在尝试将自定义 table 加入客户集合。 自定义 table 是:

+---------+--------------------+
| 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);

我可能是错的,但我认为因为 customer/customer 是 EAV 模型而不是平面模型,所以它使用的 table 别名是 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();