Api 平台不坚持 OneToMany 关系

问题描述 投票:0回答:1

我正在使用 Doctrine Inheritance 来使用 API 平台,但在保留 OneToMany 关系时遇到问题。

curl --location 'http://localhost/api/dummies' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data '{
  "fakeField": "string",
  "metadata": [
    {
      "name": "string",
      "value": "string"
    }
  ]
}'

响应将元数据作为空数组返回,并且对象未保留在表中。

{
  "id": "21f2051e-4dbe-4992-976d-ba3421c6aece",
  "fakeField": "string",
  "metadata": []
}

实体定义:

// ./src/Entity/AbstractMetadataAwareEntity.php

#[ORM\Entity]
#[ORM\Table(name: 'an_objects')]
#[ORM\InheritanceType('JOINED')]
#[ORM\DiscriminatorColumn(name: 'entity_type', type: 'string')]
#[ORM\DiscriminatorMap([
    'user' => User::class,
    'dummy' => Dummy::class,
])]
class AbstractMetadataAwareEntity
{
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'CUSTOM')]
    #[ORM\CustomIdGenerator(class: UuidGenerator::class)]
    #[ORM\Column(type: 'uuid', unique: true)]
    #[Groups(['base:read'])]
    protected UuidV6 $id;

    #[ORM\OneToMany(mappedBy: 'owner', targetEntity: Metadata::class, cascade: ['persist'])]
    #[Groups(['base:read', 'base:write'])]
    protected Collection $metadata;

    public function __construct()
    {
        $this->metadata = new ArrayCollection();
    }

    public function getId(): UuidV6
    {
        return $this->id;
    }

    /**
     * @return Collection<int, Metadata>
     */
    public function getMetadata(): Collection
    {
        return $this->metadata;
    }

    public function addMetadata(Metadata $metadata): self
    {
        if (!$this->metadata->contains($metadata)) {
            $this->metadata->add($metadata);
            $metadata->setOwner($this);
        }

        return $this;
    }

    public function removeMetadata(Metadata $metadata): self
    {
        if ($this->metadata->removeElement($metadata)) {
            // set the owning side to null (unless already changed)
            if ($metadata->getOwner() === $this) {
                $metadata->setOwner(null);
            }
        }

        return $this;
    }
}
// ./src/Entity/Dummy.php

#[ORM\Entity(repositoryClass: DummyRepository::class)]
#[ApiResource(
    normalizationContext: [
        'groups' => ['base:read', 'dummy:read']
    ],
    denormalizationContext: [
        'groups' => ['base:write', 'dummy:write']
    ]
)]
class Dummy extends AbstractMetadataAwareEntity
{
    #[ORM\Column(length: 255)]
    #[Groups(['dummy:read', 'dummy:write'])]
    private ?string $fakeField = null;

    public function getFakeField(): ?string
    {
        return $this->fakeField;
    }

    public function setFakeField(string $fakeField): self
    {
        $this->fakeField = $fakeField;

        return $this;
    }
}
// ./src/Entity/Metadata.php

#[ORM\Entity(repositoryClass: MetadataRepository::class)]
#[ORM\Table(name: 'an_metadatas')]
class Metadata
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    #[Groups(['base:read', 'base:write'])]
    private ?string $name = null;

    #[ORM\Column(length: 255)]
    #[Groups(['base:read', 'base:write'])]
    private ?string $value = null;

    #[ORM\ManyToOne(inversedBy: 'metadata')]
    #[ORM\JoinColumn(nullable: false)]
    private ?AbstractMetadataAwareEntity $owner = null;

    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 getValue(): ?string
    {
        return $this->value;
    }

    public function setValue(string $value): self
    {
        $this->value = $value;

        return $this;
    }

    public function getOwner(): ?AbstractMetadataAwareEntity
    {
        return $this->owner;
    }

    public function setOwner(?AbstractMetadataAwareEntity $owner): self
    {
        $this->owner = $owner;

        return $this;
    }
}

这个实现使用了Doctrine Inheritance

预期结果是将用户和元数据对象正确持久化到各自的表中

php symfony doctrine api-platform.com symfony-serializer
1个回答
0
投票

这个问题有更新吗? 我也遇到同样的情况... 没有错误,但嵌入关系上没有持久数据。 谢谢,

© www.soinside.com 2019 - 2024. All rights reserved.