无法删除在 Symfony / Doctrine 的联结表中具有相关条目的实体

问题描述 投票:0回答:1

我有以下实体:

公司简介 标签 标签类别 个人资料标签

ProfileTag 是一个联结表,它在 Profile 和 Tag 之间形成多对多的关系。 每个标签都属于一个类别。

我在删除给定配置文件的所有标签条目时遇到问题,当我这样做时会产生以下错误:

执行查询时发生异常:SQLSTATE[23000]: 违反完整性约束:1451 无法删除或更新父级 row:外键约束失败 (

app
.
profile_tag
, CONSTRAINT
FK_AD2B1EBCBAD26311
外键 (
tag_id
) 参考资料
tag
(
id
))

以下是我的实体:

<?php

#[ORM\Entity(repositoryClass: ProfileRepository::class)]
#[ORM\Table(name: '`profile`')]
class Profile
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\OneToMany(targetEntity: ProfileTag::class, mappedBy: 'profile', cascade: ['persist'])]
    private Collection $tag;


    public function getTag(): Collection
    {
        $tags = new ArrayCollection();
        foreach ($this->tag as $profileTag) {
            $tags->add($profileTag->getTag());
        }
        return $tags;
    }

    public function getTagByCategory(string $name): Collection
    {
        $tags = new ArrayCollection();
        foreach ($this->tag as $profileTag) {
            if ($profileTag->getTag()->getTagCategory()->getName() === $name) {
                $tags->add($profileTag->getTag());
            }
        }
        return $tags;
    }
}
?>

和标签:

<?php

namespace App\Entity;

#[ORM\Entity(repositoryClass: TagRepository::class)]
#[ORM\Table(name: '`tag`')]
class Tag
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(type: 'string', length: 32, unique: false)]
    private ?string $name = '';

    #[ORM\ManyToOne(targetEntity: TagCategory::class, inversedBy: 'Tag')]
    #[ORM\JoinColumn(nullable: false)]
    private ?TagCategory $tagCategory = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTagCategory(): ?TagCategory
    {
        return $this->tagCategory;
    }

    public function setTagCategory(?TagCategory $tagCategory): self
    {
        $this->tagCategory = $tagCategory;
        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): self
    {
        $this->name = $name;
        return $this;
    }
}
?>

和标签类别:

<?php

namespace App\Entity;

use App\Repository\TagCategoryRepository;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: TagCategoryRepository::class)]
#[ORM\Table(name: '`tag_category`')]
class TagCategory
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(type: 'string', length: 16, unique: false)]
    private ?string $name = '';

    #[ORM\Column(type: 'string', length: 32, unique: false)]
    private ?string $label = '';

    #[ORM\OneToMany(targetEntity: Tag::class, mappedBy: 'tagCategory', cascade: ['persist'])]
    private ?Collection $tag = null;

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

    public function getLabel(): ?string
    {
        return $this->label;
    }

    public function setLabel(string $label): static
    {
        $this->label = $label;
        return $this;
    }

    public function getTag(): ?Collection
    {
        return $this->tag;
    }

    public function setTag(Collection $tag): static
    {
        $this->tag = $tag;
        return $this;
    }
}
?>

最后是 ProfileTag,连接表:

<?php

namespace App\Entity;

use App\Repository\ProfileTagRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ProfileTagRepository::class)]
#[ORM\Table(name: '`profile_tag`')]
class ProfileTag
{
    #[ORM\Id]
    #[ORM\ManyToOne(targetEntity: Profile::class, inversedBy: 'profileTag')]
    #[ORM\JoinColumn(nullable: false)]
    private ?Profile $profile = null;

    #[ORM\Id]
    #[ORM\ManyToOne(targetEntity: Tag::class, inversedBy: 'profileTag')]
    #[ORM\JoinColumn(nullable: false)]
    private ?Tag $tag = null;

    public function getProfile(): ?Profile
    {
        return $this->profile;
    }

    public function setProfile(?Profile $profile): self
    {
        $this->profile = $profile;
        return $this;
    }

    public function getTag(): ?Tag
    {
        return $this->tag;
    }

    public function setTag(?Tag $tag): self
    {
        $this->tag = $tag;
        return $this;
    }
}
?>

我尝试在控制器中删除如下:

<?php

        $tags = $profile->getTag();
        foreach ($tags as $tag) {
            $this->entityManager->remove($tag);
        }
        $this->entityManager->flush();

?>

如何安全地删除标签,包括它在联结表中的条目?

symfony doctrine-orm doctrine
1个回答
0
投票

您好,尝试输入属性 $profile 和 $tag

#[ORM\JoinColumn(nullable: true)]

而不是 ProfileTag 中的 false

© www.soinside.com 2019 - 2024. All rights reserved.