我正在尝试创建/发布一个新用户及其城市(已经存在的实体对象),这是一种多对一的关系。即使我设置了非规范化组,当我在 swagger 视图中测试它时,城市属性也不会显示。知道我做错了什么吗?
API 平台 Swagger:
{
"email": "string",
"password": "string",
"firstname": "string",
"lastname": "string",
"age": 0,
"availStartDate": "2020-06-21T14:41:38.712Z",
"availEndDate": "2020-06-21T14:41:38.712Z",
"images": [
{
"filename": "string",
"title": "string",
"caption": "string",
"image": "string"
}
]
}
这是用户实体:
/**
* @ApiResource(
* iri="http://schema.org/User",
* collectionOperations={"get"={
* "normalization_context"={"groups"={"user:read", "user:item:get"}},
* },
* "post"={
* "validation_groups"={"Default", "create"}
* }},
* itemOperations={"get"={
* "normalization_context"={"groups"={"user:read", "user:item:get"}}
* }, "put"},
* normalizationContext={"groups"={"user:read"}, "swagger_definition_name"="Read"},
* denormalizationContext={"groups"={"user:write"}, "swagger_definition_name"="Write"},
* attributes={
* "pagination_items_per_page"=10
* }
* )
* @ApiFilter(PropertyFilter::class)
*
* @UniqueEntity(fields={"email"})
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"user:read", "message:read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:read", "user:write", "city:read"})
* @Assert\NotBlank()
* @Assert\Email(
* message = "The email entered is not a valid email."
* )
*/
private $email;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user:write"})
*
*/
private $password
/**
* @ORM\ManyToOne(targetEntity=City::class, inversedBy="localUsers", fetch="EAGER")
* @ORM\JoinColumn(nullable=true)
* @Groups({"user:read", "user:write"})
*
*/
private $city;
......
public function getCity(): ?City
{
return $this->city;
}
public function setCity(?City $city): self
{
$this->city = $city;
return $this;
}
我现在才看完文章,如果你还没有解决:
将以下属性添加到 $city 属性: #[ApiProperty(可读:true,可写:true)]
也许能解决问题