具有相同ID“ X”的物料(Mage_Sales_Model_Order)已存在


12

在Magento的管理区域中创建货件var/report后,在查看管理订单网格页面时(默认的Magento崩溃页面之后),该文件夹中显示以下错误:

Item (Mage_Sales_Model_Order) with the same id "1234" already exist

通过在app/code/core/Mage/Eav/Model/Entity/Collection/Abstact.php第662行的varien对象选择器中添加DISTINCT子句,可以消除此错误。但是,由于某些原因(查询速度非常慢,编辑核心文件等),我确实不想这样做。

在保持数据完整性的同时解决此问题的最佳方法是什么。我应该删除订单吗?我将不胜感激任何建设性的投入,尤其是首先要引起冲突的原因。谢谢。

-编辑-

为了清晰起见,这是完整的堆栈跟踪记录/指出我的愚蠢之处:

a:5:{i:0;s:67:"Item (Mage_Sales_Model_Order) with the same id "1234" already exist";i:1;s:4829:"#0 lib/Varien/Data/Collection/Db.php(576): Varien_Data_Collection->addItem(Object(Mage_Sales_Model_Order))
#1 app/code/core/Mage/Adminhtml/Block/Widget/Grid.php(533): Varien_Data_Collection_Db->load()
#2 app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php(61): Mage_Adminhtml_Block_Widget_Grid->_prepareCollection()
#3 app/code/core/Mage/Adminhtml/Block/Widget/Grid.php(626): Mage_Adminhtml_Block_Sales_Order_Grid->_prepareCollection()
#4 app/code/core/Mage/Adminhtml/Block/Widget/Grid.php(632): Mage_Adminhtml_Block_Widget_Grid->_prepareGrid()
#5 app/code/core/Mage/Core/Block/Abstract.php(862): Mage_Adminhtml_Block_Widget_Grid->_beforeToHtml()
#6 app/code/core/Mage/Core/Block/Abstract.php(582): Mage_Core_Block_Abstract->toHtml()
#7 app/code/core/Mage/Core/Block/Abstract.php(526): Mage_Core_Block_Abstract->_getChildHtml('grid', true)
#8 app/code/core/Mage/Adminhtml/Block/Widget/Grid/Container.php(77): Mage_Core_Block_Abstract->getChildHtml('grid')
#9 app/design/adminhtml/default/default/template/widget/grid/container.phtml(36): Mage_Adminhtml_Block_Widget_Grid_Container->getGridHtml()
#10 app/code/core/Mage/Core/Block/Template.php(241): include('/var/www/html/m...')
#11 app/code/core/Mage/Core/Block/Template.php(272): Mage_Core_Block_Template->fetchView('adminhtml/defau...')
#12 app/code/core/Mage/Core/Block/Template.php(286): Mage_Core_Block_Template->renderView()
#13 app/code/core/Mage/Adminhtml/Block/Template.php(81): Mage_Core_Block_Template->_toHtml()
#14 app/code/core/Mage/Adminhtml/Block/Widget/Container.php(308): Mage_Adminhtml_Block_Template->_toHtml()
#15 app/code/core/Mage/Core/Block/Abstract.php(863): Mage_Adminhtml_Block_Widget_Container->_toHtml()
#16 app/code/core/Mage/Core/Block/Text/List.php(43): Mage_Core_Block_Abstract->toHtml()
#17 app/code/core/Mage/Core/Block/Abstract.php(863): Mage_Core_Block_Text_List->_toHtml()
#18 app/code/core/Mage/Core/Block/Abstract.php(582): Mage_Core_Block_Abstract->toHtml()
#19 app/code/core/Mage/Core/Block/Abstract.php(526): Mage_Core_Block_Abstract->_getChildHtml('content', true)
#20 app/design/adminhtml/default/default/template/page.phtml(74): Mage_Core_Block_Abstract->getChildHtml('content')
#21 app/code/core/Mage/Core/Block/Template.php(241): include('/var/www/html/m...')
#22 app/code/core/Mage/Core/Block/Template.php(272): Mage_Core_Block_Template->fetchView('adminhtml/defau...')
#23 app/code/core/Mage/Core/Block/Template.php(286): Mage_Core_Block_Template->renderView()
#24 app/code/core/Mage/Adminhtml/Block/Template.php(81): Mage_Core_Block_Template->_toHtml()
#25 app/code/core/Mage/Core/Block/Abstract.php(863): Mage_Adminhtml_Block_Template->_toHtml()
#26 app/code/core/Mage/Core/Model/Layout.php(555): Mage_Core_Block_Abstract->toHtml()
#27 app/code/core/Mage/Core/Controller/Varien/Action.php(390): Mage_Core_Model_Layout->getOutput()
#28 app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php(95): Mage_Core_Controller_Varien_Action->renderLayout()
#29 app/code/core/Mage/Core/Controller/Varien/Action.php(419): Mage_Adminhtml_Sales_OrderController->indexAction()
#30 app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('index')
#31 app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#32 app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#33 app/Mage.php(683): Mage_Core_Model_App->run(Array)
#34 index.php(71): Mage::run('base', 'website')
#35 {main}";s:3:"url";s:72:"/index.php/admin/sales_order/index/key/0b7375aca52608483edc0cf879bd4361/";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:5:"admin";}

8
FWIW我讨厌这个愚蠢的错误,花了比我更愿意接受调试的时间。
philwinkle 2014年

您可以包括您的收藏查询或PHP代码吗?
Sukeshini

Answers:


10

您向集合中添加了一些内容,这将重复项添加到查询结果中。

magento对查询结果的作用是从每一行生成对象,然后将这些项目添加到集合中。如果该项目已经存在,则会引发此错误。

解决方案:无论您添加了什么联接,都要检查结果是否不同。

如果您在之前添加了更多字段sales_flat_order,则这些字段可能会有所不同,因此请注意使用过滤了它们DISTINCT


6

我要添加一个答案,因为我还无法添加评论。我同意@fabian-但是我不鼓励使用DISTINCTGROUP BY,如果计划正确,通常可以将联接过滤为单个不同的结果。

例如(我知道这是一个简单的例子):

如果您要使用来收集订单,sales/flat_order并且您加入了客户的地址,则有两条记录存储了帐单地址交货地址。要解决此问题,可以指定他们想要加入的地址类型。

听起来与您的情况类似,但这种情况除外。


3

我们的系统中也遇到了一些问题。我们已经分析了问题,找到了我的问题Grid.php(例如:/namespace/modulename/Block/Adminhtml/Sales/Order/Grid.php)

我们_prepareCollection()Grid.php文件中找到了功能。

错误编码为:

//Error coding
$collection->join(array('payment' => 'sales/order_payment'), 'main_table.entity_id = parent_id', 'method')
            ->getSelect()
            ->where("`payment`.`method` like '%paypal%'");

解:

//Solution coding
$collection->join(array('payment' => 'sales/order_payment'), 'main_table.entity_id = parent_id', 'method')
            ->getSelect()
            ->where("`payment`.`method` like '%paypal%'")
            ->group('method');

我们已经使用->group('value')了过滤功能。现在工作正常... !!!


3

我知道这是一个古老的问题,并且已经回答了,但是我想添加我的答案,因为我认为它可以帮助其他面临类似问题的人。我试图解释这种错误的一般原因。

首先,Mage_Sales_Model_Order在标准的Magento安装中,绝对不要出现此类错误。因此,我怀疑此错误是由第三方扩展(观察者,钩子方法,重写等)引起的,或者由不正确的代码自定义导致的数据损坏。当谈论起“怪异”作用的核心组件时,该distinct解决方案甚至不应该存在,因为数据本来就永远不会被破坏!首先要比修改代码更好地修复数据本身。

当我构建自定义EAV实体类型时,我遇到了类似的问题,并且一切正常,直到我编辑了其中一个实体。不知何故,该编辑导致该实体的文本属性之一的值重复,即在文本值表中同一实体的同一属性的两行。仅查询custom_entity表的原始查询看起来完全正常,没有重复项(怎么回事?主键约束已经确保了完整性),因此问题出在其他地方。

正如您在加载集合时可能已经猜到的那样,由于用于从数据库检索行并考虑到实体的同一属性有两个相同的文本值的联接,因此它检索了两个具有相同entity_id值但不同的行value_id

通常,当出现此类错误时,您应该怀疑数据库中的数据已被破坏,或者在最终查询中进行的联接将从数据库中检索几乎两行,从而重复了集合的唯一标识符。项目。

希望这可以帮助。

PS:关于为什么在我的情况下重复的eav文本值,请在此处查看Marius的评论http://inchoo.net/magento/creating-an-eav-based-models-in-magento/


0

这是我的代码在遇到同样的问题后运行良好:

    $collection = Mage::getResourceModel('sales/order_grid_collection');
    $prefix = Mage::getConfig()->getTablePrefix();  

    $collection->getSelect()->joinleft(array('order'=> $prefix.'sales_flat_order'),'order.entity_id=main_table.entity_id',array('pdeliverydate'));   

    $collection->getSelect()->joinleft(array('address'=> $prefix.'sales_flat_order_address'),'address.parent_id=main_table.entity_id',array('telephone'))->group('entity_id');

    $this->setCollection($collection);
    return parent::_prepareCollection();

我添加了-> group('entity_id')来解决问题。


已经建议1年之前(magento.stackexchange.com/a/139898/46249)... -1。
sv3n

-1

您必须尝试覆盖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();
    }
...

1
这不是解决方案,而只是摆脱错误消息的一种方法。
Niels

1
压榨错误与解决错误不同,请考虑为您的收集结果添加不同的内容或将联接查询按字段分组。
DWils
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.