Swagger API平台无法发布关系id

Swagger API平台无法发布关系id,swagger,symfony4,api-platform.com,Swagger,Symfony4,Api Platform.com,我对API平台非常陌生,目前,我正在尝试映射实体。我有一个实体称为“用户”实体,另一个实体称为“信用”实体。他们之间的关系是“一对一”的关系。我想要实现的是,每当我从用户帖子和更新中调用时,我都可以修改信用和vise vasa。目前,我 可以从用户“PUT”更新信用实体,如下所示。当我设置credit实体的“amount”时,它会很好地将用户id存储在credit表中 但当我尝试从信贷实体,我不能在用户ID中添加,而是它只接受金额 这是我的用户实体 <?php namespace A

我对API平台非常陌生,目前,我正在尝试映射实体。我有一个实体称为“用户”实体,另一个实体称为“信用”实体。他们之间的关系是“一对一”的关系。我想要实现的是,每当我从用户帖子和更新中调用时,我都可以修改信用和vise vasa。目前,我 可以从用户“PUT”更新信用实体,如下所示。当我设置credit实体的“amount”时,它会很好地将用户id存储在credit表中

但当我尝试从信贷实体,我不能在用户ID中添加,而是它只接受金额

这是我的用户实体

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



首先删除下划线。再一次。。。其余部分看起来不错,因此您可能需要使用类似
/api/users/{id}/credit
的路由。首先删除下划线。再一次。。。其余部分看起来不错,因此您可能需要使用类似
/api/users/{id}/credit
的路由。
<?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;
    }
}