【问题标题】:Api plateform denormalization on custom operation problem关于自定义操作问题的api平台规范化
【发布时间】:2020-11-18 01:47:31
【问题描述】:

我有一个实体凭证,其中包含自定义操作名称 add_voucher:

<?php
/**
 * @ApiResource(
 *     collectionOperations={
 *          "add_voucher"={
 *                 "access_control"="is_granted('ROLE_COMMERCIAL')",
 *                 "method"="POST",
 *                 "path"="/vouchers/add-new",
 *                 "controller"=AddVoucherAction::class,
 *                  "denormalization_context"={
 *                      "groups"={"add_new_voucher"}
 *               },
 *                  "validation_groups"={"Default", "add_voucher_validation"}
 *         },
 *     }
 * )
 * @ORM\Entity(repositoryClass="App\Repository\VoucherRepository", repositoryClass=VoucherRepository::class)
 */
class Voucher
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @Groups("add_new_voucher")
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private $code;
    /**
     * @Groups("add_new_voucher")
     * @ORM\Column(type="integer")
     */
    private $discount;
    /**
     * @Groups("add_new_voucher")
     * @ORM\Column(type="datetime")
     */
    private $starts_at;

    public function getDiscount()
    {
        return $this->discount;
    }
    public function setDiscount($discount): void
    {
        $this->discount = $discount;
    }
    public function getId(): ?int
    {
        return $this->id;
    }
    public function getCode(): ?string
    {
        return $this->code;
    }
    public function setCode(string $code): self
    {
        $this->code = $code;
        return $this;
    }
    public function getStartsAt(): ?\DateTimeInterface
    {
        return $this->starts_at;
    }
    public function setStartsAt(\DateTimeInterface $starts_at): self
    {
        $this->starts_at = $starts_at;
        return $this;
    } 

}

我将非规范化组添加到 code,discount,starts_at, I 稍后在操作中验证此列,因此我需要先在非规范化上下文中添加它,但在它只显示的招摇中 代码和折扣属性

【问题讨论】:

    标签: symfony swagger api-platform.com denormalization


    【解决方案1】:

    这是因为您的 $starts_at 属性和 getStartsAt() getter 的拼写组合。基本上,Symfony 序列化器使用你的 getter 来访问私有属性。

    只需将您的属性替换为 $starstAt 或在您的 getter 中添加下划线即可。

    看看this;

    【讨论】:

    • 我尝试在 getter 方法中添加下划线,如下所示:getStarts_At 但它不起作用,我将属性名称更改为 starstAt 并且它起作用了!谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    相关资源
    最近更新 更多