mysql - Magento 向左连接

标签 mysql magento zend-framework left-join

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

+---------+--------------------+
| 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 模型而不是平面模型,它使用的表别名是 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();

关于mysql - Magento 向左连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41812767/

相关文章:

Magento 与 Nginx 和 Varnish - 302、cookie 和重定向?

java - 如何从 Java 连接到 SOAP 服务器并运行我的服务器内置方法/服务?

mysql 不是一个好的搜索引擎

PHP MySQL - 查找行 ID

php - 在 mysql 查询中使用变量作为列名

php - 为什么 Zend_Db_Adapter 比 mysql_query 慢很多

zend-framework - Zend框架1.8推荐的注册插件的方式?

php - 为以下四个表编写连接查询,如果满足某个条件则获取记录

magento - 如何从 Magento 中的 SKU 获取产品的 Url

magento - 以编程方式从布局中删除 block