具有关联关系的API平台非规范化自定义操作

时间:2020-07-29 08:30:33

标签: php symfony api-platform.com denormalization

我有2个具有OneToOne关系的凭证和客户端(扩展了用户实体),凭证中有一个自定义操作,我想在此操作中对客户端实体进行非规范化(以便能够验证属性稍后),但不会在正式文档中显示

凭证实体:

<?php

/**
 * @ApiResource(
 *     collectionOperations={
 *          "add_voucher"={
 *                 "access_control"="is_granted('ROLE_COMMERCIAL')",
 *                 "method"="POST",
 *                 "path"="/vouchers/add-new",
 *                 "controller"=AddVoucherAction::class,
 *                 "security_post_denormalize_message"="Sorry, Only Commercials can Generate Vouchers",
 *                  "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")
     * @OneToOne(targetEntity="App\Entity\Client")
     * @JoinColumn(name="client_id", referencedColumnName="id")
     */
    private $client;

    public function getDiscount()
    {
        return $this->discount;
    }

    public function setDiscount($discount): void
    {
        $this->discount = $discount;
    }

    public function getClient()
    {
        return $this->client;
    }

    public function setClient($client): void
    {
        $this->client = $client;
    }

    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;
    }

}

客户实体:

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\ClientRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Entity\Language;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ApiResource(
 *     collectionOperations={
 *              "post"={
 *                      "method"="POST",
 *                      "validation_groups"={"registerValidation"},
 *                  }
 *     },
 *     denormalizationContext={"groups"={"register"}}
 * )
 * @ORM\Entity(repositoryClass=ClientRepository::class)
 */
class Client extends User
{
    /**
     * @Groups("register")
     * @ORM\ManyToOne(targetEntity=Language::class, inversedBy="client")
     */
    private $language;
    /**
     * @Groups({"register","add_new_voucher"})
     * @Assert\NotBlank(groups="registerValidation")
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $country;

    /**
     * @Groups("register")
     * @Assert\NotBlank(groups="registerValidation")
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $currency;

    /**
     * @Groups("register")
     * @Assert\NotBlank(groups="registerValidation")
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $timezone;

    /**
     * @Groups("register")
     * @Assert\NotBlank(groups="registerValidation")
     * @ORM\Column(type="integer", nullable=true)
     */
    private $phone;

    /**
     * Client constructor.
     * @param $language
     * @param $country
     * @param $currency
     * @param $timezone
     * @param $phone
     */
    public function __construct($language, $country, $currency, $timezone, $phone)
    {
        parent:: __construct();
        $this->language = $language;
        $this->country = $country;
        $this->currency = $currency;
        $this->timezone = $timezone;
        $this->phone = $phone;
    }

    public function getLanguage()
    {
        return $this->language;
    }

    public function setLanguage($language): void
    {
        $this->language = $language;
    }


    public function getCountry(): ?string
    {
        return $this->country;
    }

    public function setCountry(?string $country): self
    {
        $this->country = $country;

        return $this;
    }

    public function getCurrency(): ?string
    {
        return $this->currency;
    }

    public function setCurrency(?string $currency): self
    {
        $this->currency = $currency;

        return $this;
    }

    public function getTimezone(): ?string
    {
        return $this->timezone;
    }

    public function setTimezone(?string $timezone): self
    {
        $this->timezone = $timezone;

        return $this;
    }

    public function getPhone(): ?int
    {
        return $this->phone;
    }

    public function setPhone(?int $phone): self
    {
        $this->phone = $phone;

        return $this;
    }

}

我试图将非规范化组添加到客户关系和客户实体的国家/地区属性中,但是大张旗鼓地将操作仅显示代码和折扣属性

0 个答案:

没有答案
相关问题