swagger - API平台无法发布关系ID
问题描述
我对 API 平台很陌生,目前,我正在尝试与实体进行映射。我有一个实体称为“用户”实体,另一个实体称为“信用”实体。而他们之间的关系是“一对一”的关系。我想要实现的是,每当我从 User post 和 update 调用时,我都可以修改 credit 和 vise vasa。目前,我可以从用户“PUT”更新信用实体,如下所示。当我设置信用实体的“金额”时,它会很好地与 user_id 一起存储在信用表中。
但是当我从信用实体尝试时,我无法添加 userId,而是只接受金额。
这是我的用户实体
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
use Fresh\VichUploaderSerializationBundle\Annotation as Fresh;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ApiResource(
* collectionOperations={
* "get" = {
* "access_control"="is_granted('ROLE_API')"
* },
* "post" ={
* "route_name"="api_users_post_collection"
* },
* "app_login"={
* "access_control"="is_granted('ROLE_API')",
* "route_name"="app_login",
* "method"="POST",
* "swagger_context" = {
* "parameters" = {
* {
* "name" = "User Login",
* "in" = "body",
* "type" = "object",
* "schema"= {
* "email" = {"type": "string"},
* "password" = {"type" : "string"},
* "example" ={
* "email" = "string",
* "password" ="string"
* }
* }
* }
* },
* "responses" = {
* "200" = {
* "description" = "You will get User IRI and PHPSESSID",
* "schema" = {
* "type" = "object",
* "required" = {
* "email",
* "password"
* },
* "properties" = {
* "user" = {
* "type" = "string"
* },
* "PHPSESSID" = {
* "type" = "string"
* },
*
* }
* }
* },
* "400" = {
* "description" = "Bad Requests"
* }
* },
* "summary" = "User Login",
* "description" = "Set User session to api platform by email and password",
* "consumes" = {
* "application/json",
* "text/html",
* },
* "produces" = {
* "application/json",
* "application/ld+json"
* }
* }
* }
* },
* itemOperations={
* "get" ={
* "access_control"="is_granted('ROLE_API')"
* },
* "put" = {
* "access_control"="is_granted('ROLE_API')"
* }
* },
* normalizationContext={
* "groups"={"user:read"},"swagger_definition_name"="Read"
* },
* denormalizationContext={
* "groups"={"user:write"},"swagger_definition_name"="Write"
* },
* shortName="User"
*
* )
* @UniqueEntity(fields={"email"})
* @UniqueEntity(fields={"contact"})
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @Vich\Uploadable
*
* @Fresh\VichSerializableClass
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Groups({"user:read", "user:write", "credit:item:get", "credit:write"})
* @Assert\Email()
* @Assert\NotBlank()
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
* @Groups({"user:write"})
* @Assert\NotBlank()
*/
private $password;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read", "user:write"})
* @Assert\NotBlank()
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read", "user:write"})
* @Assert\NotBlank()
*/
private $lastName;
/**
* @var string provide in YYYY-MM-DD (neglect Time)
* @ORM\Column(type="date")
* @Groups({"user:read", "user:write"})
* @Assert\NotBlank()
*/
private $dob;
/**
* @ORM\Column(type="text")
* @Groups({"user:read", "user:write"})
* @Assert\NotBlank()
*/
private $address;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read", "user:write"})
* @Assert\NotBlank()
* @Assert\Length(
* min=8,
* max=8,
* maxMessage="contact number must have 8 character",
* minMessage="contact number must have 8 character"
* )
*/
private $contact;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Credit", mappedBy="user_id", cascade={"persist"}, orphanRemoval=true)
* @Groups({"user:read", "user:write"})
* @Assert\Valid()
*/
private $credit;
/**
* @var \DateTime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @var \DateTime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private $updated_at;
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getDob(): ?\DateTimeInterface
{
return $this->dob;
}
public function setDob(\DateTimeInterface $dob): self
{
$this->dob = $dob;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(string $address): self
{
$this->address = $address;
return $this;
}
public function getContact(): ?string
{
return $this->contact;
}
public function setContact(string $contact): self
{
$this->contact = $contact;
return $this;
}
public function getCredit(): ?Credit
{
return $this->credit;
}
public function setCredit(?Credit $credit): self
{
$this->credit = $credit;
// set (or unset) the owning side of the relation if necessary
$newUser_id = $credit === null ? null : $this;
if ($newUser_id !== $credit->getUserId()) {
$credit->setUserId($newUser_id);
}
return $this;
}
public function getCreated()
{
return $this->created_at;
}
public function getUpdated()
{
return $this->updated_at;
}
}
这是我的信用实体
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ApiResource(
* collectionOperations={
* "get",
* "post"
* },
* itemOperations={
* "get" = {
* "normalization_context"={"groups"={"credit:read", "credit:item:get"}},
* },
* "put"
* },
* shortName="credit",
* normalizationContext={"groups"={"credit:read"}, "swagger_definition_name"="Read"},
* denormalizationContext={"groups"={"credit:write"}, "swagger_definition_name"="Write"},
* )
* @ORM\Entity(repositoryClass="App\Repository\CreditRepository")
*/
class Credit
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"credit:read", "credit:write", "user:read", "user:write"})
*/
private $amount;
/**
* @ORM\OneToOne(targetEntity="App\Entity\User", inversedBy="credit")
* @Groups({"credit:read", "credit:write"})
* @ORM\JoinColumn(nullable=false)
* @Assert\Valid()
*/
private $user_id;
/**
* @var \DateTime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @var \DateTime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private $updated_at;
public function getId(): ?int
{
return $this->id;
}
public function getAmount(): ?string
{
return $this->amount;
}
public function setAmount(string $amount): self
{
$this->amount = $amount;
return $this;
}
public function getUserId(): ?User
{
return $this->user_id;
}
public function setUserId(?User $user_id): self
{
$this->user_id = $user_id;
return $this;
}
public function getCreated()
{
return $this->created_at;
}
public function getUpdated()
{
return $this->updated_at;
}
}
当我调用信用实体时,我怎样才能实现这一点,我可以添加 user_id (这是信用的所有者)?
解决方案
推荐阅读
- jmeter - 如何在jmeter的html报告中添加http状态码
- git - Git:将一些文件/目录保存在本地而不是推送到远程
- amazon-web-services - S3 现在可以安全地用作实时视频的来源吗?
- javascript - 使用
- node.js - DynamoDB 与活动表的连接在本地工作,但在 ECS 中不工作
- python - 在两个不同版本的 Python 上安装同一个包
- angular - 打到最后一个退格键时如何不去掉标签输入芯片?
- python - 使用具有已实现功能的 Pandas DF
- sql - 在 SQL Server 中将值转换为列
- java - 我必须为活动充值才能显示自定义适配器 [Andoid Studio]