作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 api-platform v3 和 symfony v5。我对显示的属性有疑问。例如,我们有简单的实体类类别:
<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ApiResource()
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="children")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="parent")
*/
private $children;
public function __construct()
{
$this->children = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection|self[]
*/
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(self $child): self
{
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}
return $this;
}
public function removeChild(self $child): self
{
if ($this->children->contains($child)) {
$this->children->removeElement($child);
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
}
现在我想显示 title 和 parent 属性以供读写。为此,我输入组注释:
<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* normalizationContext={"groups" = {"category:read"}},
* denormalizationContext={"groups" = {"category:write"}}
* )
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
//...
/**
* @ORM\Column(type="string", length=255)
* @Groups({"category:read", "category:write"})
*/
private $title;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="children")
* @Groups({"category:read", "category:write"})
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="parent")
*/
private $children;
//...
}
之后,我们在 swagger 文档中看到了“title”属性。但是“parent”属性是不可见的:
为什么我在“示例值”部分看不到“父”属性?
作为临时解决方案,我描述了 documentation swagger:
<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* collectionOperations={
* "get",
* "post" = {
* "sequrity" = "is_granted('ROLE_ADMIN')",
* "openapi_context" = {
* "requestBody" = {
* "content" = {
* "application/json" = {
* "schema" = {
* "type" = "object",
* "required" = {
* "title"
* },
* "properties" = {
* "title" = {
* "type" = "string"
* },
* "parent" = {
* "type" = "string"
* }
* }
* }
* }
* }
* }
* }
* }
* },
* normalizationContext={"groups" = {"category:read"}},
* denormalizationContext={"groups" = {"category:write"}}
* )
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
//...
}
我想知道如何正确地做这件事。为什么组不适用于“父”属性?
最佳答案
现在遇到了同样的问题。尝试为 $parent 属性使用注解 @ApiProperty(readableLink=false)。
/**
* @ORM\ManyToOne(targetEntity=CounterpartCategory::class, inversedBy="children")
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
* @Groups({"category:read", "category:write"})
* @ApiProperty(readableLink=false)
*/
private ?self $parent;
关于api-platform.com - "Parent"属性没有出现在 "Example Value"与 denormalizationContext 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59660715/
我使用 api-platform v3 和 symfony v5。我对显示的属性有疑问。例如,我们有简单的实体类类别: children = new ArrayCollection(); }
我是一名优秀的程序员,十分优秀!