如何使用 Api 平台和 Angular 记录嵌入关系?

时间:2021-04-28 05:52:49

标签: angular symfony api-platform.com

他是我的观点:我有两个实体主题和编队。关系是 manyToOne :一个主题可以有多个编队,一个编队属于一个主题。 我希望能够创建一个新的编队并为这个新的编队选择现有的主题之一。我可以用 IRI 做到这一点,但我无法用 denormalisationContext 找到它。这是我的表:

形成

/**
 * @ApiResource(
 *     normalizationContext={"groups"={"formation:read"}},
 *     denormalizationContext={"groups"={"formation:write"}},
 * )
 * @ORM\Entity(repositoryClass=FormationRepository::class)
 */
class Formation
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @Groups({"formation:read","formation:write"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"formation:read","formation:write"})
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=500, nullable=true)
     * @Groups({"formation:read","formation:write"})

    /**
     * @ORM\ManyToOne(targetEntity=Theme::class, inversedBy="formation")
     * @Groups({"formation:read","formation:write"})
     */
    private $theme;

主题


/**
 * @ApiResource()
 * 
 * @ORM\Entity(repositoryClass=ThemeRepository::class)
 */
class Theme
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"formation:read","formation:write"})
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=1000, nullable=true)
     * 
     * 
     */
    private $description;

    /**
     * @ORM\OneToMany(targetEntity=Formation::class, mappedBy="theme", orphanRemoval=true)
     */
    private $formation;

在 Angular 方面,我有一个经典的响应式表单,我写的主题字段:

                            <option *ngFor="let theme of themes" [value]="theme.name">
                                {{theme.name}}
                            </option>

但是当我尝试在 Formation 上发出 post 请求时,它不起作用:( 如果有其他人知道为什么吗?

提前非常感谢 我

0 个答案:

没有答案