正在尝试插入数据manytone FOSRestBundle
我正在尝试使用bundle FOSRestBundle(SF5)创建一个API REST。 我有一个实体“category”,它可以有一个父“category”。 以下是实体:正在尝试插入数据manytone FOSRestBundle,rest,symfony,fosuserbundle,Rest,Symfony,Fosuserbundle,我正在尝试使用bundle FOSRestBundle(SF5)创建一个API REST。 我有一个实体“category”,它可以有一个父“category”。 以下是实体: <?php namespace App\Entity\Main; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use JMS\Serializer\Annotation\Expose; use
<?php
namespace App\Entity\Main;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\ExclusionPolicy;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\CategorieRepository")
* @ORM\Table(name="categorie")
* @ExclusionPolicy("all")
*/
class Categorie
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @Expose
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string
* @Assert\NotBlank()
* @Expose
* @ORM\Column(type="string", length=255)
*/
private $libelle;
/**
* @var string
* @Assert\NotBlank()
* @Expose
* @ORM\Column(type="string", length=255)
*/
private $icone;
/**
* @var Categorie
* @ORM\ManyToOne(targetEntity="App\Entity\Main\Categorie", inversedBy="categories", cascade={"all"}, fetch="EAGER")
* @ORM\JoinColumn(name="categorie_parent_id", referencedColumnName="id", nullable=true)
*/
private $categorieParent;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="App\Entity\Main\Categorie", mappedBy="categorieParent")
*/
private $categories;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="App\Entity\Main\Produit", mappedBy="categorie")
*/
private $produits;
public function __construct()
{
$this->produits = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLibelle(): ?string
{
return $this->libelle;
}
public function setLibelle(string $libelle): self
{
$this->libelle = $libelle;
return $this;
}
public function getIcone(): ?string
{
return $this->icone;
}
public function setIcone(string $icone): self
{
$this->icone = $icone;
return $this;
}
public function setCategorieParent(Categorie $categorieParent): self
{
$this->categorieParent = $categorieParent;
return $this;
}
public function getCategorieParent(Categorie $categorieParent)
{
return $this->categorieParent;
}
}
当我使用postman插入包含此内容的数据时:
{
"libelle":"Blonde",
"icone":"blonde.png",
"categorieParent.id": 1
}
插入了“libelle”和“icone”,但未设置“categorieParent”。
我试过:
{
"libelle":"Blonde",
"icone":"blonde.png",
"categorieParent": 1
}
对于每次尝试,我都用数字和字符串设置id
什么都不管用。
Thx的帮助:) 分类租金只接受分类实体;libelle和icone之所以有效,是因为它们是简单的字符串。您应该使用传递的整数获取实体,然后保存值。CategorieParent将只接受一个Categorie实体;libelle和icone之所以有效,是因为它们是简单的字符串。您应该使用传递的整数获取实体,然后保存值
{
"libelle":"Blonde",
"icone":"blonde.png",
"categorieParent": 1
}
{
"libelle":"Blonde",
"icone":"blonde.png",
"categorieParent": {
"id": 1
}
}