Magento 2 Rest API,PUT 目录请求不起作用

问题描述 投票:0回答:1

我正在使用 Symfony 应用程序向 Magento 2 Rest Api 发送请求。当我发送 PUT 请求来更新目录时,它不会更改 Magento 中的目录信息,但会更改 Symfony 端除parentId 之外的名称和其他参数。

这是带有请求的代码:

public function updateCategory($id, $name, $parentId, $isActive = true)
{
    $pageSuffix = "rest/V1/categories/" . $id;
    $headers = ['content-type' => "application/json", 'Authorization' => "Bearer " . $this->adminToken];
    $content = ["category" => ["id" => (int)$id, "name" => $name, "parentId" => (int)$parentId, "isActive" => (bool)$isActive]];
    $response = $this->buzz->put($this->urlBase . $pageSuffix, $headers, json_encode($content));
    print_r(json_decode($response));
    print_r($this->buzz->getLastRequest());
} 

发送请求之前的类别树:

[id] => 1
[parent_id] => 0
[name] => Root Catalog
[is_active] => 
[position] => 0
[level] => 0
[product_count] => 0
[children_data] => Array
    (
        [0] => stdClass Object
            (
                [id] => 2
                [parent_id] => 1
                [name] => root_test_category_1
                [is_active] => 1
                [position] => 1
                [level] => 1
                [product_count] => 0
                [children_data] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 3
                                [parent_id] => 2
                                [name] => test_category_1
                                [is_active] => 1
                                [position] => 1
                                [level] => 2
                                [product_count] => 0
                                [children_data] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [id] => 8
                                                [parent_id] => 3
                                                [name] => test_category_1_1
                                                [is_active] => 1
                                                [position] => 1
                                                [level] => 3
                                                [product_count] => 0
                                                [children_data] => Array
                                                    (
                                                        [0] => stdClass Object
                                                            (
                                                                [id] => 9
                                                                [parent_id] => 8
                                                                [name] => test_category_1_1_1
                                                                [is_active] => 1
                                                                [position] => 1
                                                                [level] => 4
                                                                [product_count] => 0
                                                                [children_data] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [1] => stdClass Object
                                                            (
                                                                [id] => 10
                                                                [parent_id] => 8
                                                                [name] => test_category_1_1_2
                                                                [is_active] => 1
                                                                [position] => 2
                                                                [level] => 4
                                                                [product_count] => 0
                                                                [children_data] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [2] => stdClass Object
                                                            (
                                                                [id] => 16
                                                                [parent_id] => 8
                                                                [name] => test_category_1_1_3
                                                                [is_active] => 1
                                                                [position] => 3
                                                                [level] => 4
                                                                [product_count] => 0
                                                                [children_data] => Array
                                                                    (
                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                            )

                    )

            )

        [1] => stdClass Object
            (
                [id] => 5
                [parent_id] => 1
                [name] => root_test_category_2
                [is_active] => 1
                [position] => 2
                [level] => 1
                [product_count] => 0
                [children_data] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 6
                                [parent_id] => 5
                                [name] => test_category_2_1
                                [is_active] => 1
                                [position] => 1
                                [level] => 2
                                [product_count] => 0
                                [children_data] => Array
                                    (
                                    )

                            )

                    )

            )

    )

然后我发送一个请求来更改 test_category_1_1_3,id 16。下面的命令发送参数,如上面列出的函数 updateCategory() 中所示。所以它应该获取 id 为 16 的目录并更改其名称和parentId。

app/console magento:test 16 test_category_1_1_3_test 6 1 -vvv

运行命令并发送请求后,我通过从 Magento Rest API 获取此目录来检查它。结果,它更改了名称,但其他一切都保持不变。

[id] => 16
[parent_id] => 8
[name] => test_category_1_1_3_test
[is_active] => 1
[position] => 3
[level] => 4
[product_count] => 0
[children_data] => Array
    (
    )

请求的响应:

Buzz\Message\Response Object
(
    [protocolVersion:Buzz\Message\Response:private] => 
    [statusCode:Buzz\Message\Response:private] => 
    [reasonPhrase:Buzz\Message\Response:private] => 
    [headers:Buzz\Message\AbstractMessage:private] => Array
        (
            [0] => HTTP/1.1 200 OK
            [1] => Date: Wed, 17 May 2017 12:56:23 GMT
            [2] => Server: Apache/2.4.18 (Ubuntu)
            [3] => Set-Cookie: PHPSESSID=56hrlb8nejsnrduk05npemd3n3; expires=Wed, 17-May-2017 13:56:23 GMT; Max-Age=3600; path=/; domain=magento-project.nikolay.localdev; HttpOnly
            [4] => Expires: Thu, 19 Nov 1981 08:52:00 GMT
            [5] => Cache-Control: no-store, no-cache, must-revalidate
            [6] => Pragma: no-cache
            [7] => X-Frame-Options: SAMEORIGIN
            [8] => Content-Length: 739
            [9] => Content-Type: application/json; charset=utf-8
        )

    [content:Buzz\Message\AbstractMessage:private] => {"id":16,"parent_id":8,"name":"test_category_1_1_3_test","is_active":true,"position":3,"level":4,"children":"","created_at":"2017-05-17 10:51:17","updated_at":"2017-05-17 10:51:17","path":"1\/2\/3\/8\/16","available_sort_by":[],"include_in_menu":true,"custom_attributes":[{"attribute_code":"display_mode","value":"PRODUCTS"},{"attribute_code":"is_anchor","value":"1"},{"attribute_code":"path","value":"1\/2\/3\/8\/16"},{"attribute_code":"children_count","value":"0"},{"attribute_code":"custom_use_parent_settings","value":"0"},{"attribute_code":"custom_apply_to_products","value":"0"},{"attribute_code":"url_key","value":"test-category-1-1-3"},{"attribute_code":"url_path","value":"test-category\/test-category-1-1\/test-category-1-1-3"}]}
)
Buzz\Message\Request Object
(
    [method:Buzz\Message\Request:private] => PUT
    [resource:Buzz\Message\Request:private] => /rest/V1/categories/16
    [host:Buzz\Message\Request:private] => http://magento-project.nikolay.localdev
    [protocolVersion:Buzz\Message\Request:private] => 1.1
    [headers:Buzz\Message\AbstractMessage:private] => Array
        (
            [0] => content-type: application/json
            [1] => Authorization: Bearer mcq2i97t0t3b15hdmwwhoolp6ma0lyuy
        )

    [content:Buzz\Message\AbstractMessage:private] => {"category":{"id":16,"name":"test_category_1_1_3_test","parentId":6,"isActive":true}}
)

此外,Magento 本身没有任何变化。

screenshot from Magento

我尝试在(vendor/magento/module-catalog/Model/CategoryRepository.php)中调试 save() 函数,但它没有向我显示任何有帮助的内容。我一直在尝试清除 Magento 和 Symfony 端的缓存,但这也没有帮助。

php symfony magento magento-rest-api
1个回答
-1
投票

已确认为错误。可能与 Magento 2 中的商店范围有关。

https://github.com/magento/magento2/issues/9949

© www.soinside.com 2019 - 2024. All rights reserved.