Symfony + Api-platform “hydra:description”: “Invalid IRI” когда пытаюсь зделать POST в БД с релейшенами

103
15 февраля 2021, 17:40
    <?php
    namespace App\Entity;
    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\Common\Collections\Collection;
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Serializer\Annotation\Groups;
    /**
     *
     * @ORM\Entity(repositoryClass="App\Repository\SpareCategoryRepository")
     */
    class SpareCategory
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         */
        private $id;
        /**
         * @ORM\Column(type="string", length=100)
         */
        private $name;
        /**
         * @Groups({"read", "write"})
         * @ORM\OneToMany(targetEntity="Spare", mappedBy="category")
         */
        private $spares;
        public function __construct()
        {
            $this->spares = new ArrayCollection();
        }
        public function getId(): ?int
        {
            return $this->id;
        }
        public function getSpares(): Collection
        {
            return $this->spares;
        }
        public function addSpare(Spare $one): self
        {
            if (!$this->spares->contains($one)) {
                $this->spares[] = $one;
                $one->setCategory($this);
            }
            return $this;
        }
        public function removeSpare(Spare $one): self
        {
            if ($this->spares->contains($one)) {
                $this->spares->removeElement($one);
                // set the owning side to null (unless already changed)
                if ($one->getCategory() === $this) {
                    $one->setCategory(null);
                }
            }
            return $this;
        }
        public function getName(): ?string
        {
            return $this->name;
        }
        public function setName(string $name): self
        {
            $this->name = $name;
            return $this;
        }
    }

    <?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\SpareRepository")
 */
class Spare
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;
    /**
     * @ORM\Column(type="string", length=50)
     */
    private $name;
    /**
     * @ORM\ManyToOne(targetEntity="SpareCategory", inversedBy="spares")
     */
    private $category;
    public function getId(): ?int
    {
        return $this->id;
    }
    public function getName(): ?string
    {
        return $this->name;
    }
    public function setName(string $name): self
    {
        $this->name = $name;
        return $this;
    }
    public function getCategory(): ?SpareCategory
    {
        return $this->category;
    }
    public function setCategory(?SpareCategory $category): self
    {
        $this->category = $category;
        return $this;
    }
}

И когда делаю POST:

{
  "spares": [
    "kakayato detal"
  ],
  "name": "Dvigatel"
}

То получаю следуйщий эрор

{
  "@context": "/api/contexts/Error",
  "@type": "hydra:Error",
  "hydra:title": "An error occurred",
  "hydra:description": "Invalid IRI \"kakayato detal\".",
  "trace": [
    {
      "namespace": "",
      "short_class": "",
      "class": "",
      "type": "",
      "function": "",
      "file": "/var/www/auto-reminder/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php",
      "line": 427,
      "args": []
    },
    {
      "namespace": "ApiPlatform\\Core\\Serializer",
      "short_class": "AbstractItemNormalizer",
      "class": "ApiPlatform\\Core\\Serializer\\AbstractItemNormalizer",
      "type": "->",
      "function": "denormalizeRelation",
      "file": "/var/www/auto-reminder/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php",
      "line": 399,
      "args": [
        [
          "string",
          "spares"
        ],
        [
          "object",
          "ApiPlatform\\Core\\Metadata\\Property\\PropertyMetadata"
        ],
        [
          "string",
          "App\\Entity\\Spare"
        ],
        [
          "string",
          "kakayato detal"
        ],
        [
          "string",
          "jsonld"
        ],
        [
          "array",
          {
            "operation_type": [
              "string",
              "collection"
            ],
            "collection_operation_name": [
              "string",
              "post"
            ],
            "api_allow_update": [
              "boolean",
              false
            ],
            "resource_class": [
              "string",
              "App\\Entity\\Spare"
            ],
            "input": [
              "null",
              null
            ],
            "output": [
              "null",
              null
            ],
            "request_uri": [
              "string",
              "/api/spare_categories"
            ],
            "uri": [
              "string",
              "http://local-dev/api/spare_categories"
            ],
            "api_denormalize": [
              "boolean",
              true
            ],
            "cache_key": [
              "string",
              "9f9d8347663bf17cd07bee285792046d"
            ]
          }
        ]
      ]
    },
    {
      "namespace": "ApiPlatform\\Core\\Serializer",
      "short_class": "AbstractItemNormalizer",
      "class": "ApiPlatform\\Core\\Serializer\\AbstractItemNormalizer",
      "type": "->",
      "function": "denormalizeCollection",
      "file": "/var/www/auto-reminder/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php",
      "line": 655,
      "args": [
        [
          "string",
          "spares"
        ],
        [
          "object",
          "ApiPlatform\\Core\\Metadata\\Property\\PropertyMetadata"
        ],
        [
          "object",
          "Symfony\\Component\\PropertyInfo\\Type"
        ],
        [
          "string",
          "App\\Entity\\Spare"
        ],
        [
          "array",
          [
            [
              "string",
              "kakayato detal"
            ]
          ]
        ],
        [
          "string",
          "jsonld"
        ],
        [
          "array",
          {
            "operation_type": [
              "string",
              "collection"
            ],
            "collection_operation_name": [
              "string",
              "post"
            ],
            "api_allow_update": [
              "boolean",
              false
            ],
            "resource_class": [
              "string",
              "App\\Entity\\Spare"
            ],
            "input": [
              "null",
              null
            ],
            "output": [
              "null",
              null
            ],
            "request_uri": [
              "string",
              "/api/spare_categories"
            ],
            "uri": [
              "string",
              "http://local-dev/api/spare_categories"
            ],
            "api_denormalize": [
              "boolean",
              true
            ],
            "cache_key": [
              "string",
              "52eb09d972b50716a61429643dfb612a"
            ]
          }
        ]
      ]
    },
    {
      "namespace": "ApiPlatform\\Core\\Serializer",
      "short_class": "AbstractItemNormalizer",
      "class": "ApiPlatform\\Core\\Serializer\\AbstractItemNormalizer",
      "type": "->",
      "function": "createAttributeValue",
      "file": "/var/www/auto-reminder/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php",
      "line": 350,
      "args": [
        [
          "string",
          "spares"
        ],
        [
          "array",
          [
            [
              "string",
              "kakayato detal"
            ]
          ]
        ],
        [
          "string",
          "jsonld"
        ],
        [
          "array",
          {
            "operation_type": [
              "string",
              "collection"
            ],
            "collection_operation_name": [
              "string",
              "post"
            ],
            "api_allow_update": [
              "boolean",
              false
            ],
            "resource_class": [
              "string",
              "App\\Entity\\Spare"
            ],
            "input": [
              "null",
              null
            ],
            "output": [
              "null",
              null
            ],
            "request_uri": [
              "string",
              "/api/spare_categories"
            ],
            "uri": [
              "string",
              "http://local-dev/api/spare_categories"
            ],
            "api_denormalize": [
              "boolean",
              true
            ],
            "cache_key": [
              "string",
              "52eb09d972b50716a61429643dfb612a"
            ]
          }
        ]
      ]
    },
    {
      "namespace": "ApiPlatform\\Core\\Serializer",
      "short_class": "AbstractItemNormalizer",
      "class": "ApiPlatform\\Core\\Serializer\\AbstractItemNormalizer",
      "type": "->",
      "function": "setAttributeValue",
      "file": "/var/www/auto-reminder/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php",
      "line": 356,
      "args": [
        [
          "object",
          "App\\Entity\\SpareCategory"
        ],
        [
          "string",
          "spares"
        ],
        [
          "array",
          [
            [
              "string",
              "kakayato detal"
            ]
          ]
        ],
        [
          "string",
          "jsonld"
        ],
        [
          "array",
          {
            "operation_type": [
              "string",
              "collection"
            ],
            "collection_operation_name": [
              "string",
              "post"
            ],
            "api_allow_update": [
              "boolean",
              false
            ],
            "resource_class": [
              "string",
              "App\\Entity\\SpareCategory"
            ],
            "input": [
              "null",
              null
            ],
            "output": [
              "null",
              null
            ],
            "request_uri": [
              "string",
              "/api/spare_categories"
            ],
            "uri": [
              "string",
              "http://local-dev/api/spare_categories"
            ],
            "api_denormalize": [
              "boolean",
              true
            ],
            "cache_key": [
              "string",
              "52eb09d972b50716a61429643dfb612a"
            ]
          }
        ]
      ]
    },
    {
      "namespace": "Symfony\\Component\\Serializer\\Normalizer",
      "short_class": "AbstractObjectNormalizer",
      "class": "Symfony\\Component\\Serializer\\Normalizer\\AbstractObjectNormalizer",
      "type": "->",
      "function": "denormalize",
      "file": "/var/www/auto-reminder/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php",
      "line": 219,
      "args": [
        [
          "array",
          {
            "spares": [
              "array",
              [
                [
                  "string",
                  "kakayato detal"
                ]
              ]
            ],
            "name": [
              "string",
              "Dvigatel"
            ]
          }
        ],
        [
          "string",
          "App\\Entity\\SpareCategory"
        ],
        [
          "string",
          "jsonld"
        ],
        [
          "array",
          {
            "operation_type": [
              "string",
              "collection"
            ],
            "collection_operation_name": [
              "string",
              "post"
            ],
            "api_allow_update": [
              "boolean",
              false
            ],
            "resource_class": [
              "string",
              "App\\Entity\\SpareCategory"
            ],
            "input": [
              "null",
              null
            ],
            "output": [
              "null",
              null
            ],
            "request_uri": [
              "string",
              "/api/spare_categories"
            ],
            "uri": [
              "string",
              "http://local-dev/api/spare_categories"
            ],
            "api_denormalize": [
              "boolean",
              true
            ],
            "cache_key": [
              "string",
              "52eb09d972b50716a61429643dfb612a"
            ]
          }
        ]
      ]
    },
    {
      "namespace": "ApiPlatform\\Core\\Serializer",
      "short_class": "AbstractItemNormalizer",
      "class": "ApiPlatform\\Core\\Serializer\\AbstractItemNormalizer",
      "type": "->",
      "function": "denormalize",
      "file": "/var/www/auto-reminder/vendor/api-platform/core/src/JsonLd/Serializer/ItemNormalizer.php",
      "line": 117,
      "args": [
        [
          "array",
          {
            "spares": [
              "array",
              [
                [
                  "string",
                  "kakayato detal"
                ]
              ]
READ ALSO
Изменение текста в PSD средствами php

Изменение текста в PSD средствами php

столкнулся со следующей задачей: Необходимо автоматизировать печать сертификата, в котором уникальные (имя, промокод, и даты действия сертификата)Сертификат...

126
PHP | Удалить тэги со строки

PHP | Удалить тэги со строки

У меня есть вот такое описание

119
Как получить курсы валют по api?

Как получить курсы валют по api?

Есть ли бесплатные способы получения курсов валютна этой странице многие методы не работают https://stackoverflow

270