【问题标题】:Magento Grid add new columnMagento Grid 添加新列
【发布时间】:2012-04-17 12:32:31
【问题描述】:
protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id',array('company'));
    $this->setCollection($collection);
}

我使用上面的代码在订单列表网格上添加了一个公司字段。 但它显示“已存在具有相同 id “1038”的项目(Mage_Sales_Model_Order)”

【问题讨论】:

    标签: magento


    【解决方案1】:

    也许可以尝试内部连接:

    $collection->getSelect()->joinInner(
        array(
            'order_address' => 'sales_flat_order_address'
        ),
        'order_address.parent_id = main_table.entity_id'
    );
    

    另外,回显您的 SQL 并查看集合返回的内容,然后尝试在您的数据库上运行该 sql。这应该可以帮助您找出自己做错了什么。

    echo $collection->getSelect()->__toString();
    

    请记住,仅此一项不会将列添加到网格中。您需要在_prepareColumns() 中添加列

    编辑: 实际上,考虑一下,内部连接可能对您没有帮助。您遇到的问题是 sales_flat_order_address 包含每个 parent_id 的多个条目,因此您需要使用 GROUP BY 或 SELECT DISTINCT 来计算重复项。试试这样的:

    $collection->getSelect()->joinInner(
        array(
            'order_address' => 'sales_flat_order_address'
        ),
        'order_address.parent_id = main_table.entity_id'
    )->group(array('entity_id', 'parent_id'));
    

    它并不完美,但您尝试做的事情本质上是不完美的,因为一个订单有多个地址。另一种方法是仅使用帐单地址或仅使用送货地址明确加入。

    【讨论】:

    • 它工作正常,但现在我的发票网格有一个小问题,我在下面的答案中提到了细节。
    猜你喜欢
    • 2016-03-13
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 2019-03-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多