Я пытаюсь создать таблицу отношений категорий, в основном имею категории, которые владеют другими категориями, такими как дети и родители
, когда я пытаюсь POST новую категорию (например, «Child-category-1») и назначьте ей владельца, она переходит в бесконечный цикл с ошибкой:
Код: Выделить всё
The total number of joined relations has exceeded the specified maximum. Raise the limit if necessary with the \"api_platform.eager_loading.max_joins\" configuration key (https://api-platform.com/docs/core/performance/#eager-loading), or limit the maximum serialization depth using the \"enable_max_depth\" option of the Symfony serializer (https://symfony.com/doc/current/components/serializer.html#handling-serialization-depth).
Код: Выделить всё
{
"@context": "/contexts/Category",
"@id": "/categories/1",
"@type": "Category",
"name": "Parent1",
"ownedUsers": [
"/category_owners/1",
"/category_owners/2",
"/category_owners/3"
],
"owners": []
}
Код: Выделить всё
{
"name" : "child-category-4",
"owners":[
"/categories/1"
]
}
- Я попробовал отключить ForceEager => не сработало.
- Я попробовал установить MaxDepth на 1 => не помогло.
Вот как выглядят объекты Category и CategoryOwner:
Код: Выделить всё
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\CategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: CategoryRepository::class)]
#[ApiResource(
normalizationContext: [
'groups' => ['category:read'],
],
denormalizationContext: [
'groups' => ['category:write'],
],
)]
class Category
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(['category:write', 'category:read'])]
private ?string $name = null;
#[ORM\OneToMany(mappedBy: 'owner', targetEntity: CategoryOwner::class,cascade: ["persist"])]
#[Groups(['category:write', 'category:read'])]
private Collection $ownedUsers;
#[ORM\OneToMany(mappedBy: 'owned', targetEntity: CategoryOwner::class,cascade: ["persist"])]
#[Groups(['category:write', 'category:read'])]
private Collection $owners;
public function __construct()
{
$this->ownedUsers = new ArrayCollection();
$this->owners = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
/**
* @return Collection
*/
public function getOwnedUsers(): Collection
{
return $this->ownedUsers;
}
public function addOwnedUser(CategoryOwner $ownedUser): static
{
if (!$this->ownedUsers->contains($ownedUser)) {
$this->ownedUsers->add($ownedUser);
$ownedUser->setOwner($this);
}
return $this;
}
public function removeOwnedUser(CategoryOwner $ownedUser): static
{
if ($this->ownedUsers->removeElement($ownedUser)) {
// set the owning side to null (unless already changed)
if ($ownedUser->getOwner() === $this) {
$ownedUser->setOwner(null);
}
}
return $this;
}
/**
* @return Collection
*/
public function getOwners(): Collection
{
return $this->owners;
}
public function addOwner(CategoryOwner $owner): static
{
if (!$this->owners->contains($owner)) {
$this->owners->add($owner);
$owner->setOwned($this);
}
return $this;
}
public function removeOwner(CategoryOwner $owner): static
{
if ($this->owners->removeElement($owner)) {
// set the owning side to null (unless already changed)
if ($owner->getOwned() === $this) {
$owner->setOwned(null);
}
}
return $this;
}
}
Код: Выделить всё
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\CategoryOwnerRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: CategoryOwnerRepository::class)]
#[ApiResource(
normalizationContext: [
'groups' => ['category:owner:read'],
],
denormalizationContext: [
'groups' => ['category:owner:write'],
],
)]
класс CategoryOwner
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
Private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'ownedUsers')]
#[ORM\JoinColumn(nullable: false)]
#[Groups(['category:owner:read', 'category:owner:write', 'category:write'])]
частная ?Category $owner = null;
#[ORM\ManyToOne(inversedBy: 'owners')]
#[ORM\JoinColumn(nullable: false)]
#[Groups(['category:owner:read', 'category:owner:write', 'category:write'])]
Private ?Category $owned = null;
public function getId(): ?int
{
return $this->id;
public function getOwner(): ?Category
{< br /> return $this->owner;
публичная функция setOwner(?Category $owner): static
{
$this->owner = $ владелец;
return $this;
публичная функция getOwned(): ?Category
{
return $this->owned ;
}
публичная функция setOwned(?Category $owned): static
{
$this->owned = $owned;
вернуть $this;
Источник: https://stackoverflow.com/questions/781 ... bo-for-api
Мобильная версия