微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

未从数据库中获取的 Api 平台 DTO 多对多补丁实体

如何解决未从数据库中获取的 Api 平台 DTO 多对多补丁实体

class PropertyInput
{
...
    /**
     * @var LivingRoom[]
     * @Groups({"property:write"})
     */
    public $livingRooms;
...
}
/**
 * @ApiResource (
 *     normalizationContext={"groups"={"livingroom:read"}},*     attributes={"security"="is_granted(constant('\\App\\Model\\UserRoles::ADMIN'))"}
 * )
 * @ORM\Entity()
 */
class LivingRoom implements ResourceInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @Groups({"property:read","property:write","livingroom:read"})
     */
    private int $id;

    /**
     * @ORM\Column(type="boolean",options={"default": 0})
     * @Groups({"property:read","livingroom:read"})
     */
    private bool $diningArea;

    /**
     * @ORM\Column(type="boolean","livingroom:read"})
     */
    private bool $sofa;

    /**
     * @ORM\Column(type="boolean","livingroom:read"})
     */
    private bool $TV;

    /**
     * @ORM\Column(type="boolean","livingroom:read"})
     */
    private bool $coffeeTable;

    /**
     * @ORM\ManyToOne(targetEntity=Property::class,inversedBy="livingRoom")
     * @ORM\JoinColumn(nullable=false,onDelete="CASCADE")
     */
    private Property $property;


    public function setId(int $id): void
    {
        $this->id = $id;
    }


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

    public function getDiningArea(): ?bool
    {
        return $this->diningArea;
    }

    public function setDiningArea(bool $diningArea): self
    {
        $this->diningArea = $diningArea;

        return $this;
    }

    public function getSofa(): ?bool
    {
        return $this->sofa;
    }

    public function setSofa(bool $sofa): self
    {
        $this->sofa = $sofa;

        return $this;
    }

    public function getTV(): ?bool
    {
        return $this->TV;
    }

    public function setTV(bool $TV): self
    {
        $this->TV = $TV;

        return $this;
    }

    public function getCoffeeTable(): ?bool
    {
        return $this->coffeeTable;
    }

    public function setCoffeeTable(bool $coffeeTable): self
    {
        $this->coffeeTable = $coffeeTable;

        return $this;
    }

    public function getproperty(): ?Property
    {
        return $this->property;
    }

    public function setProperty(?Property $property): self
    {
        $this->property = $property;

        return $this;
    }
}

Property 和 LivingRoom 之间存在多对多关系。在创建和修补属性时,我们需要使用 DTO 方法。 当我们尝试使用 DTO 修补属性时,客厅 ID 不会从请求中解析或识别,因此每次都会创建新的客厅。

以下是修补属性请求的示例:

{"id":26,"propertyName":"Palace","bathrooms":0,"floors":0,"bedrooms":0,"livingRooms":[{"id":1,"TV":true,"coffeeTable": false,"diningArea":true,"sofa":true} ]}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。