Проблема Symfony с отправкой данных в базу данных

#php #symfony #api-platform.com

Вопрос:

У меня есть 3 объекта (песня, инструмент и категория).

Мой урок песни такой :

 lt;?php  namespace AppEntity;  use DoctrineORMMapping as ORM; use AppRepositorySongRepository; use ApiPlatformCoreAnnotationApiResource; use SymfonyComponentSerializerAnnotationGroups;  /**  * @ORMEntity(repositoryClass=SongRepository::class)  * @ApiResource()  */ class Song {  /**  * @ORMId  * @ORMGeneratedValue  * @ORMColumn(type="integer")  * @Groups("song:write")  */  private $id;   /**  * @ORMColumn(type="string", length=255, nullable=true)  * @Groups("song:write")  */  private $sheet;   /**  * @ORMColumn(type="string", length=255, nullable=true)  * @Groups("song:write")  */  private $lyrics;   /**  * @ORMColumn(type="string", length=255, nullable=true)  * @Groups("song:write")  */  private $video;   /**  * @ORMManyToOne(targetEntity=Instrument::class, inversedBy="songs")  */  private $instrument;   /**  * @ORMManyToOne(targetEntity=Category::class, inversedBy="songs")  */  private $category;   public function getId(): ?int  {  return $this-gt;id;  }   public function getSheet(): ?string  {  return $this-gt;sheet;  }   public function setSheet(?string $sheet): self  {  $this-gt;sheet = $sheet;   return $this;  }   public function getLyrics(): ?string  {  return $this-gt;lyrics;  }   public function setLyrics(?string $lyrics): self  {  $this-gt;lyrics = $lyrics;   return $this;  }   public function getVideo(): ?string  {  return $this-gt;video;  }   public function setVideo(?string $video): self  {  $this-gt;video = $video;   return $this;  }   public function getInstrument(): ?instrument  {  return $this-gt;instrument;  }   public function setInstrument(?instrument $instrument): self  {  $this-gt;instrument = $instrument;   return $this;  }   public function getCategory(): ?category  {  return $this-gt;category;  }   public function setCategory(?category $category): self  {  $this-gt;category = $category;   return $this;  } }  

Я использую Почтальона, чтобы протестировать и попытаться опубликовать новую песню.

Проблема в том, что я не могу отправлять значения инструментов и категорий в базу данных.

Если я не использую группы, все значения будут равны нулю.

Если я включу инструмент и категорию в группы в классе песни, их значения будут равны нулю.

Если я включу всех в группы, у меня будет ошибка : Недопустимое значение IRI»значение».

Мой контроллер выглядит так :

 lt;?php  namespace AppController;  use AppEntitySong; use DoctrineORMEntityManagerInterface; use SymfonyComponentHttpFoundationRequest; use SymfonyComponentRoutingAnnotationRoute; use SymfonyComponentSerializerSerializerInterface; use SymfonyBundleFrameworkBundleControllerAbstractController;  class AddSongController extends AbstractController {  /**  * @Route("/api/songs", name="add_song", methods={"POST"})  */  public function addSong(Request $request, SerializerInterface $serializer, EntityManagerInterface $em)  {  $jsonSong = $request-gt;getContent();    $postSong = $serializer-gt;deserialize($jsonSong, Song::class, 'json', ['groups' =gt; 'song:write']);   $em-gt;persist($postSong);  $em-gt;flush();   return $this-gt;json($postSong, 201, []);  } }  

Комментарии:

1. у вас есть объявленный ресурс api для всех ваших сущностей, вы связаны? если нет, вы столкнетесь с проблемой iri.