Search code examples
shopwareshopware6

Create a new category in Shopware 6 get error /0/translations/2fbb5fe2e29a4d70aa5854ce7ce3e20b/name


When trying to create a new category, I get this error.

/0/translations/2fbb5fe2e29a4d70aa5854ce7ce3e20b/name This value should not be blank.

This is my array:(As you can see. My error is related to the key's name. But the key's name has a value)

[
  0 => [
  "categories" => [
    "id" => "7767b163a5124c83ad8f6c701ffabade",
    "parentId" => null,
    "name" => "Supply",
    "path" => "professional",
    "level" => 5,
    "active" => true,
    "description" => null,
    "translations" => [
       "2fbb5fe2e29a4d70aa5854ce7ce3e20b" =>[
         "defaultLanguageId"=>"2fbb5fe2e29a4d70aa5854ce7ce3e20b",
         "name" => "Professional",
         "metaTitle" => "",
         "metaDescription" => "",
         "description" => "",
       ],
       "f7e5c2a4bb194c339714f9798d8f853d" => [
         "name" => "Materiale Profession",
         "defaultLanguageId"=>"2fbb5fe2e29a4d70aa5854ce7ce3e20b",
         "metaTitle" => "",
         "metaDescription" => "",
         "description" => "",
        ],
        "51d36b57c6144235bcb0332a3ca20a5c" => [
          "name" => "Profesional",
         "defaultLanguageId"=>"2fbb5fe2e29a4d70aa5854ce7ce3e20b",
          "metaTitle" => "",
          "metaDescription" => "",
          "description" => "",
        ],
        "66623e62998e42c695fba0d22b023c63" =>[
          "name" => "Professionnel",
         "defaultLanguageId"=>"2fbb5fe2e29a4d70aa5854ce7ce3e20b",
          "metaTitle" => "",
          "metaDescription" => "",
          "description" => "",
        ],
      ]
    ]
 ]]

Also, it doesn't work without translation. I don't have a problem with creating products.

 $this->productRepository->create($products, $context);

I get this error only when I'm going to create the category

 $this->categoryRepository->create($category, $context);

Does anyone know where my problem is?


Solution

  • I think the reason for the error is the extra categories array key in your example. The repository expects a list of associative arrays with the fields of the entities as keys, that means in your example you only provide the categories field, which is no valid category payload.

    Additionally in your case you could greatly simplify your code as it seems that you only want to update the name in different languages.

    You can try the following payload:

    [
      0 => [
        "id" => "7767b163a5124c83ad8f6c701ffabade",
        "parentId" => null,
        "name" => [
          "2fbb5fe2e29a4d70aa5854ce7ce3e20b" => "Professional",
          "f7e5c2a4bb194c339714f9798d8f853d" => "Materiale Profession",
            ...
        ],
        "path" => "professional",
        "level" => 5,
        "active" => true,
        "description" => null,
      ]
    ]
    

    Notice the missing array index categories.