php - Api 平台非规范化自定义操作与关系
问题描述
我有 2 个具有 OneToOne 关系的实体凭证和客户端(扩展用户实体),我在凭证实体中有一个自定义操作,我想在此操作中对客户端实体进行非规范化(以便以后能够验证属性)但是它不会显示在 swagger 文档中
凭证实体:
<?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;
}
}
我试图将非规范化组添加到客户关系和客户实体中的国家/地区属性,但大摇大摆地操作仅显示代码和折扣属性
解决方案
推荐阅读
- c# - 使用 CancellationToken 与 Task.Run、Task.Wait 混淆
- http - HA 代理使用 tcp-check 进行 HTTP 连接
- windbg - 如何知道任何应用程序处于挂起状态,然后用户将其杀死或用户通常将其杀死或发生任何崩溃?
- node.js - 如何在 cloudinary 中搜索图像?
- python - 如何使用 heroku 发布我的博客!sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)没有这样的表:帖子
- docker - 在 docker-compose 中使用 docker:dind
- angular - 需要使用 Angular 9 嵌套 ng 内容
- arduino - Digispark micro + SSD1306 - 编译错误
- php - Cookie 在 PHP 中无法识别
- octobercms - 检查标签是否在数组中(十月 CMS 博客)