带有消息“具有相同ID“ X”的商品(Mage_Sales_Model_Order)的Magento API“异常”已存在”

J P*_*ana 4 magento magento-1.9

我正在尝试使用Magento REST API获取订单列表。

我们使用的REST请求非常基本:http//www.example.com/api/rest/orders

响应显示下一个错误:

{
  "messages": {
    "error": [
      {
        "code": 0,
        "message": "Item (Mage_Sales_Model_Order) with the same id \"54\" already exist"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

检查我的异常日志以查看发生了什么并获得错误的下一个回溯:

2015-09-10T21:54:59+00:00 ERR (3):
exception 'Exception' with message 'Item (Mage_Sales_Model_Order) with the same id "54" already exist' in /path/to/site/lib/Varien/Data/Collection.php:373
Stack trace:
#0 /path/to/site/lib/Varien/Data/Collection/Db.php(576): Varien_Data_Collection->addItem(Object(Mage_Sales_Model_Order))
#1 /path/to/site/lib/Varien/Data/Collection.php(301): Varien_Data_Collection_Db->load()
#2 /path/to/site/app/code/core/Mage/Sales/Model/Api2/Order.php(302): Varien_Data_Collection->getItems()
#3 /path/to/site/app/code/core/Mage/Api2/Model/Resource.php(245): Mage_Sales_Model_Api2_Order->_retrieveCollection()
#4 /path/to/site/app/code/core/Mage/Api2/Model/Dispatcher.php(74): Mage_Api2_Model_Resource->dispatch()
#5 /path/to/site/app/code/core/Mage/Api2/Model/Server.php(239): Mage_Api2_Model_Dispatcher->dispatch(Object(Mage_Api2_Model_Request), Object(Mage_Api2_Model_Response))
#6 /path/to/site/app/code/core/Mage/Api2/Model/Server.php(107): Mage_Api2_Model_Server->_dispatch(Object(Mage_Api2_Model_Request), Object(Mage_Api2_Model_Response), Object(Mage_Api2_Model_Auth_User_Admin))
#7 /path/to/site/api.php(67): Mage_Api2_Model_Server->run()
#8 {main}
Run Code Online (Sandbox Code Playgroud)

我修改了文件app / code / core / Mage / Sales / Model / Api2 / Order.php(函数_retrieveCollection),并添加了一行以在日志上打印一些信息:

Mage::log($collection->getSelect(),null,'mylog.log');
Run Code Online (Sandbox Code Playgroud)

这是输出的一部分:

    [_parts:protected] => Array
        (
            [straightjoin] =>
            [distinct] =>
            [columns] => Array
                (
                    [0] => Array
                        (
                            [0] => main_table
                            [1] => *
                            [2] =>
                        )

                    [1] => Array
                        (
                            [0] => payment_method
                            [1] => method
                            [2] => payment_method
                        )

                    [2] => Array
                        (
                            [0] => gift_message
                            [1] => sender
                            [2] => gift_message_from
                        )

                    [3] => Array
                        (
                            [0] => gift_message
                            [1] => recipient
                            [2] => gift_message_to
                        )

                    [4] => Array
                        (
                            [0] => gift_message
                            [1] => message
                            [2] => gift_message_body
                        )

                    [5] => Array
                        (
                            [0] => order_tax
                            [1] => title
                            [2] => tax_name
                        )

                    [6] => Array
                        (
                            [0] => order_tax
                            [1] => percent
                            [2] => tax_rate
                        )

                )

            [union] => Array
                (
                )

            [from] => Array
                (
                    [main_table] => Array
                        (
                            [joinType] => from
                            [schema] =>
                            [tableName] => sales_flat_order
                            [joinCondition] =>
                        )

                    [payment_method] => Array
                        (
                            [joinType] => left join
                            [schema] =>
                            [tableName] => sales_flat_order_payment
                            [joinCondition] => main_table.entity_id = payment_method.parent_id
                        )

                    [gift_message] => Array
                        (
                            [joinType] => left join
                            [schema] =>
                            [tableName] => gift_message
                            [joinCondition] => main_table.gift_message_id = gift_message.gift_message_id
                        )

                    [order_tax] => Array
                        (
                            [joinType] => left join
                            [schema] =>
                            [tableName] => sales_order_tax
                            [joinCondition] => main_table.entity_id = order_tax.order_id
                        )

                )

            [where] => Array
                (
                )

            [group] => Array
                (
                )

            [having] => Array
                (
                )

            [order] => Array
                (
                )

            [limitcount] =>
            [limitoffset] =>
            [forupdate] =>
        )

    [_tableCols:protected] => Array
        (
        )

)
Run Code Online (Sandbox Code Playgroud)

如果我正确理解,则意味着SQL语句类似于:

SELECT 
    main_table.*,
    payment_method.method AS method,
    gift_message.sender AS gift_message_from,
    gift_message.recipient AS gift_message_to,
    gift_message.message AS gift_message_body,
    order_tax.title AS tax_name,
    order_tax.percent AS tax_rate
 FROM
 sales_flat_order AS main_table LEFT JOIN
 sales_flat_order_payment AS payment_method ON main_table.entity_id = payment_method.parent_id LEFT JOIN
 gift_message ON main_table.gift_message_id = gift_message.gift_message_id LEFT JOIN
 sales_order_tax AS order_tax ON main_table.entity_id = order_tax.order_id
Run Code Online (Sandbox Code Playgroud)

手动运行上一个查询后,它提出了多个具有相同entity_id(sales_flat_order)的行。这些“重复的” entity_id行在以后使用时似乎是问题Varien_Data_Collection->addItem

使具有相同entity_id的两行都出现在结果集上的查询部分是LEFT JOIN sales_order_tax。该表包含的每个订单可包含N行,因为每一行都包含一个不同的税法。

例如,在加拿大,我们针对某些地区收集了两种不同的税收规则。在不列颠哥伦比亚省,我们收取5%的商品及服务税(GST)(特定国家/地区),再加上7%的PST(省)。

我是否在这里遗漏了明显的东西,还是遇到了错误?

非常感谢您的帮助,感谢您的阅读!

PS我的问题非常接近此处所述的问题:Magento API V2销售订单列表不起作用

J P*_*ana 5

玩了一会儿之后,我想我找到了一个解决方案,所以将其发布在这里,以备将来参考。

我们需要修改集合的查询,以按sales_flat_order.entity_id进行分组,然后再开始迭代Mage_Sales_Model_Api2_Order :: __ retrieveCollection中的项目。

由于修改核心文件不是一个好习惯,因此我们可以将核心类复制到本地代码池,然后根据需要对其进行修改。Magento将使用本地文件夹下的类覆盖核心类。

  1. 将app / code / core / Mage / Sales / Model / Api2 / Order.php复制到app / code / local / Mage / Sales / Model / Api2 / Order.php
  2. 修改本地代码池文件夹下的文件(app / code / local / Mage / Sales / Model / Api2)
  3. 寻找功能_retrieveCollection(Magento 1.9.2中的第288行)
  4. 在该行之后,$this->_addTaxInfo($collection);您应该添加: $collection->getSelect()->group('main_table.entity_id');

缺点

如果要更新Magento,我们想将当前版本的Mage_Sales_Model_Api2_Order类与新版本进行比较,如果发现差异,我们将重复在本地代码池文件夹下复制核心文件的过程,然后再次执行该版本。 。