具有相同 ID “429”的项目 (Mage_Sales_Model_Quote) 已存在


Item (Mage_Sales_Model_Quote) with the same id "429" already exist

每当我将报告/quote_collection与销售/quote_item表联接时,我都会收到此错误。我正在尝试为未注册用户创建一个废弃的购物车报告。我需要显示废弃购物车中的所有物品。

请参阅下面的代码:

$this->addFieldToFilter('items_count', array('neq' => '0'))
     ->addFieldToFilter('main_table.is_active', '1')
     ->addSubtotal($storeIds, $filter)
     ->setOrder('updated_at');
$this->getSelect()->joinInner(
     array('quote_items' => $this->getTable('sales/quote_item')),
            'quote_items.quote_id = main_table.entity_id',
            'name');
if (is_array($storeIds) && !empty($storeIds)) {
     $this->addFieldToFilter('store_id', array('in' => $storeIds));
}
return $this;

如何解决此错误?有没有办法显示所有项目?

当 Magento 尝试将同一项添加到集合两次时,会出现此消息,因为 JOIN 查询结果多次包含它。

您可以使用主键上的 GROUP BY 来避免这种情况,如下所示:

$collection->getSelect()->group('e.entity_id')