3

我的 Grid.php 文件中有以下代码:

function _prepareCollection () {
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(
    array('sfog' => 'sales_flat_order_grid'),
    'main_table.entity_id = sfog.entity_id',
    array('sfog.shipping_name','sfog.billing_name')
);
$collection->getSelect()->joinLeft(
    array('sfo'=>'sales_flat_order'),
    'sfo.entity_id=main_table.entity_id',
    array(
        'sfo.customer_email',
        'sfo.weight',
        'sfo.discount_description',
        'sfo.increment_id',
        'sfo.store_id',
        'sfo.created_at',
        'sfo.status',
        'sfo.base_grand_total',
        'sfo.grand_total'
    )
);

我也想添加表 sales_order_item,但是如果我添加此表,我会收到此错误:

已存在具有相同 ID“119”的项目 (Mage_Sales_Model_Order)

无论如何围绕这个?

4

4 回答 4

5

假设你加入了soi意义sales_order_itemsoi.item_id通过做分组

$collection->getSelect()->group('soi.item_id');
于 2012-06-06T09:39:16.120 回答
4

您必须将 sales_flat_order_item 表的 entity_id 列设为集合的 entity_id 列。您还必须重命名 main_table 类的 entity_id 列以避免entity_id列冲突

$collection->getSelect()->joinLeft(
   array('sfoi' => 'sales_flat_order_item'),
   'sfoi.order_id=sfo.entity_id', 
   array(
      'entity_id' => 'sfoi.item_id',  // Re-assign entity_id column
      'main_table_entity_id' => 'main_table.entity_id'  // Rename original entity_id column
   )
)
于 2012-08-02T20:45:54.787 回答
2

显然,加入 sales_order_item 会导致每个订单返回多行(不足为奇),并且 Magento 正在尝试将行数据加载到专门用于订单的集合中,其中包含一个检查以确保每个订单只发生一次。该检查正在产生您看到的错误。

要解决此问题,您可以:

  • 将集合类更改为“sales/order_item_collection”,它允许每个订单项目一行

-或者-

  • 不加入 sales_order_item,而是通过以下方式从每个订单中检索订单商品集getItemsCollection()
于 2012-06-08T23:28:20.130 回答
1

这是因为 Auto increment value,它被用作一个id,它是唯一标识记录的

于 2012-06-04T16:11:56.783 回答