【问题标题】:Magento JoinLeftMagento 加入左
【发布时间】:2017-01-23 18:03:42
【问题描述】:

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

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

【问题讨论】:

    标签: mysql magento zend-framework left-join


    【解决方案1】:

    我可能是错的,但我认为因为 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();
    

    【讨论】:

    • 你完全正确。情况就是这样。表别名确实是e。感谢您的明确答复。
    猜你喜欢
    • 1970-01-01
    • 2010-12-01
    • 2011-03-14
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 2014-02-11
    相关资源
    最近更新 更多