Magento Reports - 产品 - 产品订购问题:具有相同ID的项目(Mage_Catalog_Model_Product)已存在

时间:2011-12-25 07:59:58

标签: magento report

问题: 在Magento管理面板中,通过报告 - 产品 - 订购的产品生成报告时,会发生错误:


    Item (Mage_Catalog_Model_Product) with the same id "45" already exist
    #0 /home/glassesm/public_html/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(236): Varien_Data_Collection->addItem(Object(Mage_Catalog_Model_Product))
    #1 /home/glassesm/public_html/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(964): Mage_Eav_Model_Entity_Collection_Abstract->addItem(Object(Mage_Catalog_Model_Product))
    #2 /home/glassesm/public_html/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(794): Mage_Eav_Model_Entity_Collection_Abstract->_loadEntities(false, false)
    #3 /home/glassesm/public_html/lib/Varien/Data/Collection.php(729): Mage_Eav_Model_Entity_Collection_Abstract->load()
    #4 /home/glassesm/public_html/app/code/core/Mage/Reports/Model/Totals.php(53): Varien_Data_Collection->getIterator()
    #5 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/Block/Report/Grid.php(368): Mage_Reports_Model_Totals->countTotals(Object(Mage_Adminhtml_Block_Report_Product_Sold_Grid), '2011-12-10 00:0...', '2011-12-10 23:5...')
    #6 /home/glassesm/public_html/app/design/adminhtml/default/default/template/report/grid.phtml(138): Mage_Adminhtml_Block_Report_Grid->getReport('2011-12-10 00:0...', '2011-12-10 23:5...')
    #7 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(216): include('/home/glassesm/...')
    #8 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(247): Mage_Core_Block_Template->fetchView('adminhtml/defau...')
    #9 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(261): Mage_Core_Block_Template->renderView()
    #10 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/Block/Template.php(81): Mage_Core_Block_Template->_toHtml()
    #11 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(758): Mage_Adminhtml_Block_Template->_toHtml()
    #12 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(525): Mage_Core_Block_Abstract->toHtml()
    #13 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(476): Mage_Core_Block_Abstract->_getChildHtml('grid', true)
    #14 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/Block/Widget/Grid/Container.php(70): Mage_Core_Block_Abstract->getChildHtml('grid')
    #15 /home/glassesm/public_html/app/design/adminhtml/default/default/template/widget/grid/container.phtml(36): Mage_Adminhtml_Block_Widget_Grid_Container->getGridHtml()
    #16 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(216): include('/home/glassesm/...')
    #17 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(247): Mage_Core_Block_Template->fetchView('adminhtml/defau...')
    #18 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(261): Mage_Core_Block_Template->renderView()
    #19 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/Block/Template.php(81): Mage_Core_Block_Template->_toHtml()
    #20 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/Block/Widget/Container.php(295): Mage_Adminhtml_Block_Template->_toHtml()
    #21 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(758): Mage_Adminhtml_Block_Widget_Container->_toHtml()
    #22 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Text/List.php(43): Mage_Core_Block_Abstract->toHtml()
    #23 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(758): Mage_Core_Block_Text_List->_toHtml()
    #24 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(525): Mage_Core_Block_Abstract->toHtml()
    #25 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(476): Mage_Core_Block_Abstract->_getChildHtml('content', true)
    #26 /home/glassesm/public_html/app/design/adminhtml/default/default/template/page.phtml(74): Mage_Core_Block_Abstract->getChildHtml('content')
    #27 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(216): include('/home/glassesm/...')
    #28 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(247): Mage_Core_Block_Template->fetchView('adminhtml/defau...')
    #29 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(261): Mage_Core_Block_Template->renderView()
    #30 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/Block/Template.php(81): Mage_Core_Block_Template->_toHtml()
    #31 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(758): Mage_Adminhtml_Block_Template->_toHtml()
    #32 /home/glassesm/public_html/app/code/core/Mage/Core/Model/Layout.php(529): Mage_Core_Block_Abstract->toHtml()
    #33 /home/glassesm/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(389): Mage_Core_Model_Layout->getOutput()
    #34 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/controllers/Report/ProductController.php(97): Mage_Core_Controller_Varien_Action->renderLayout()
    #35 /home/glassesm/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Adminhtml_Report_ProductController->soldAction()
    #36 /home/glassesm/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(253): Mage_Core_Controller_Varien_Action->dispatch('sold')
    #37 /home/glassesm/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
    #38 /home/glassesm/public_html/app/code/core/Mage/Core/Model/App.php(340): Mage_Core_Controller_Varien_Front->dispatch()
    #39 /home/glassesm/public_html/app/Mage.php(627): Mage_Core_Model_App->run(Array)
    #40 /home/glassesm/public_html/index.php(80): Mage::run('', 'store')
    #41 {main}

你能帮我解决这个问题吗?

6 个答案:

答案 0 :(得分:6)

实际上我在2台不同的服务器上有2个不同的商店,并且都有magento 1.6。我在两家商店都有这个问题。 检查数据库,那里没有双重条目。

我的解决方案是:

修改:/www/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php
行:256

替换:return parent::addItem($object);

使用:try { return parent::addItem($object); } catch (Exception $ex) { }

它完美无缺,但欢迎任何其他解决方案。

答案 1 :(得分:4)

这是因为您的产品系列会返回重复的产品ID,您可以通过对'e.entity_id'进行分组来对产品系列进行分组

试试这个

$collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', array('in' => $uniqueIds));

<?php $collection->getSelect()->group('e.entity_id');?>

答案 2 :(得分:1)

它说你有两个具有相同ID的产品。这可能发生在您使用导入或批量更新产品时。

简单的解决方案是找到ID为35的产品,并删除它,因为它是重复的,或者更改ID,因为它必须是唯一的。请注意,您应该查看所有相关表,而不仅仅是产品表。问题可能在于您的EAV属性表或任何其他数据表。

答案 3 :(得分:0)

在同一地点得到同样的错误,这就是为什么以及如何修复。

Magento订购产品报告从“sales_flat_order_item”构建“列表”,然后加入“目录/产品”以获取有关产品的更多信息(sku,类型,选项,attribute_set等)。

在我的情况下,3个产品从商店中删除,当Magento构建列表并加入目录/产品时,我上面列出的所有信息都是NULL。当Report模块构建集合时,在方法addItem中,它尝试在集合数组中将ID设置为键,在那些已删除的产品中没有ID,因此设置了NULL。

想象一下构建报告的这组项目。

ID - 名称:1,2,3,DELETED ITEM,4

当“DELETE ITEM”添加到集合中时,将如下所示:

Array(
    1 => Object,
    2 => Object,
    3 => Object,
    4 => Object (DELETED ITEM)
)

这是因为在addItem中,当没有ID Magento时,在数组中添加一个带有auto incrementmenet的新位置:

if (!is_null($itemId)) {
    .
    .
    .
} else {
    $this->_items[] = $item;
}

然后,当集合添加下一个项目(ID:4)时,将抛出您正在查看的异常:

if (isset($this->_items[$itemId])) {
    throw new Exception('Item ('.get_class($item).') with the same id "'.$item->getId().'" already exist');
}

更改addItem的工作方式非常危险!!收集在Magento中随处可用,添加 trycatch 块肯定会影响您的商店在其他地方/模块中的工作方式。以下是我修复的方法:

您可以使用自定义模块复制到本地或ovewrite,由您决定:

在“Reports_Product / Collection”中添加新方法,仅过滤“现有产品”

应用程序/代码/本地/法师/报告/型号/ Mysql4 /产品/ Collection.php

public function addNotDeletedProductRestrition(){
    $this->getSelect()->where('order_items.product_id is NOT NULL');

    return $this;
}

在ORDERED Collection中调用此方法。

应用程序/代码/本地/法师/报告/型号/ Mysql4 /产品/有序/ Collection.php

protected function _joinFields($from = '', $to = '')
{
    $this->addAttributeToSelect('*')
        ->addOrderedQty($from, $to)
        ->setOrder('ordered_qty', 'desc')
        ->addNotDeletedProductRestrition();

    return $this;
}

我放在了_joinFields里面,但你可以随时随地打电话。

抱歉我生锈的英语,我没有太多时间复习,这个错误已经带走了我很多。

答案 4 :(得分:-3)

您还可以覆盖引发此异常的连接类Mage_Sales_Model_Resource_Order_Grid_Collection::addItem

<?php
class Fixed_Order_Grid_Collection extends Mage_Sales_Model_Resource_Order_Grid_Collection{

    public function addItem(Varien_Object $item)
    {
        $itemId = $this->_getItemId($item);

        if (!is_null($itemId)) {
            if (isset($this->_items[$itemId])) {
                //throw new Exception('Item ('.get_class($item).') with the same id "'.$item->getId().'" already exist');
            }
            $this->_items[$itemId] = $item;
        } else {
            $this->_addItem($item);
        }
        return $this;
    }
}
class MyCompony_MyExtention_Block_Adminhtml_OrderGrid extends Mage_Adminhtml_Block_Widget_Grid
{
     public function __construct()
    {
        parent::__construct();
        $this->setId('sales_order_grid');
        $this->setUseAjax(true);
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
    }
    protected function _getCollectionClass()
    {
        return 'sales/order_grid_collection';
    }

    protected function _prepareCollection()
    {
        $collection = new Fixed_Order_Grid_Collection();
        $select = $collection->getSelect();
        $select->join('sales_flat_order_item AS order_item', 'order_item.order_id=main_table.entity_id','quote_item_id',NULL);
        $select->distinct();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }
...

答案 5 :(得分:-6)

只需转到您的Adminpanel,打开产品(第一行显示ID)并保存即可。在所有商店视图中执行此操作。现在问题应该消失了。