orm - Symfony 和 Api-Platform:发布问题并使用多对多关系
问题描述
我有 2 个具有 ManyToMany 关系的实体 Foo 和 Bar:
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ApiResource(
* normalizationContext={"groups"={"foo:read"}},
* denormalizationContext={"groups"={"foo:write"}},
* collectionOperations={
* "get",
* "post"={"security"="is_granted('ROLE_DEVROOT')"}
* },
* itemOperations={
* "get",
* "put"={"security"="is_granted('ROLE_DEVROOT')"},
* "delete"={"security"="is_granted('ROLE_DEVROOT')"},
* }
* )
* @ORM\Entity(repositoryClass="App\Repository\FooRepository")
* @Gedmo\SoftDeleteable()
* @UniqueEntity(fields={"label"}, message="There is already a PTUR with this label")
*/
//Direct access from API (No DTO)
class Foo
{
use TimestampableEntity;
use SoftDeleteableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"foo:read", "foo:write"})
*/
private $id;
...
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Bar", inversedBy="foos", cascade="all")
* @ORM\JoinTable(
* name="footobar",
* joinColumns={@ORM\JoinColumn(name="foo_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={@ORM\JoinColumn(name="bar_id", referencedColumnName="id", nullable=false)}
* )
* @Groups({"foo:read", "foo:write"})
*/
private $bars;
...
public function __construct()
{
$this->bars = new ArrayCollection();
}
...
/**
* @return Collection|Bar[]
* @Groups({"foo:read", "foo:write"})
*/
public function getBars(): Collection
{
return $this->bars;
}
public function addBars(Bar $bars): self
{
if (!$this->bars->contains($bars)) {
$this->bars[] = $bars;
}
return $this;
}
public function removeBars(Bar $bars): self
{
if ($this->bars->contains($bars)) {
$this->bars->removeElement($bars);
}
return $this;
}
...
public function __toString(){
return $this->label;
}
}
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\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ApiResource(
* normalizationContext={"groups"={"bar:read"}},
* denormalizationContext={"groups"={"bar:write"}},
* collectionOperations={
* "get",
* "post"={"security"="is_granted('ROLE_DEVROOT')"}
* },
* itemOperations={
* "get",
* "put"={"security"="is_granted('ROLE_DEVROOT')"},
* "delete"={"security"="is_granted('ROLE_DEVROOT')"},
* }
* )
* @ORM\Entity(repositoryClass="App\Repository\BarRepository")
* @Gedmo\SoftDeleteable()
* @UniqueEntity(fields={"label"}, message="There is already a PTU with this label")
*/
//Direct access from API (No DTO)
class Bar
{
use TimestampableEntity;
use SoftDeleteableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"bar:read", "bar:write"})
*/
private $id;
...
/**
* @ORM\Column(type="boolean")
* @Groups({"bar:read", "bar:write"})
*/
private $isdefault = false;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Foo", mappedBy="bars")
* @Groups({"bar:read", "bar:write"})
*/
private $foos;
public function __construct()
{
$this->foos = new ArrayCollection();
}
...
public function __toString(){
return $this->label;
}
/**
* @return Collection|Foo[]
* @Groups({"bar:read", "bar:write"})
*/
public function getFoos(): Collection
{
return $this->foos;
}
public function addFoo(Foo $foo): self
{
if (!$this->foos->contains($foo)) {
$this->foos[] = $foo;
$foo->addBars($this);
}
return $this;
}
public function removeFoo(Foo $foo): self
{
if ($this->foos->contains($foo)) {
$this->foos->removeElement($foo);
$foo->removeBars($this);
}
return $this;
}
}
当我在 Foo 上发出 POST 或 PUT 请求时,交叉表未填充(使用 phpmyadmin 检查)(JSON):
{"id":3,"label":"ccccc","bars":["/xxx/api/bars/1","/xxx/api/bars/3"],"enrangebase":true,"启用":true,"isdefault":false}
我错过了什么?有没有办法跟踪错误?
谢谢您的帮助,
EDIT1:添加规范化组 EDIT2:将规范化组添加到 Bar
解决方案
推荐阅读
- reactjs - 如何使 MUI AppBar 中的搜索组件居中?
- apache-camel - 如何多次向同一组路由发送消息并在不同线程上以异步方式同时执行它?
- qt - 属性的动画问题
- ssl - Letencrypt 设置到 GCP CDN
- wordpress - .htaccess:如何将 WordPress 页面/部分重写为子域?
- c# - Xunit 测试测试资源管理器未运行测试
- java - 通知访问允许但切换未更改
- c++ - 是否可以在 C++ 中创建自己的宏以供 QMake 稍后解决?
- ios - 为什么 url.bookmarkData 返回 nil?
- javascript - 具有二维数组的角度复选框