I have the following entities:
Profile
Tag
TagCategpory
ProfileTag
ProfileTag is a junction table which forms a mahy to many relationship between Profile and Tag. Each Tag belobgs to a catepory.
I am having trouble removing all tag entries for a given profile, the fopllowing error is being produced when I do:
An exception occurred while executing a query: SQLSTATE[23000]:
Integrity constraint violation: 1451 Cannot delete or update a parent
row: a foreign key constraint fails (app
.profile_tag
, CONSTRAINT
FK_AD2B1EBCBAD26311
FOREIGN KEY (tag_id
) REFERENCEStag
(id
))
Below are my entities:
<?php
#[ORMEntity(repositoryClass: ProfileRepository::class)]
#[ORMTable(name: '`profile`')]
class Profile
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMOneToMany(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;
}
}
?>
And Tag:
<?php
namespace AppEntity;
#[ORMEntity(repositoryClass: TagRepository::class)]
#[ORMTable(name: '`tag`')]
class Tag
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMColumn(type: 'string', length: 32, unique: false)]
private ?string $name = '';
#[ORMManyToOne(targetEntity: TagCategory::class, inversedBy: 'Tag')]
#[ORMJoinColumn(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;
}
}
?>
And Tag Category:
<?php
namespace AppEntity;
use AppRepositoryTagCategoryRepository;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
#[ORMEntity(repositoryClass: TagCategoryRepository::class)]
#[ORMTable(name: '`tag_category`')]
class TagCategory
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMColumn(type: 'string', length: 16, unique: false)]
private ?string $name = '';
#[ORMColumn(type: 'string', length: 32, unique: false)]
private ?string $label = '';
#[ORMOneToMany(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;
}
}
?>
And Finally ProfileTag, the junction table:
<?php
namespace AppEntity;
use AppRepositoryProfileTagRepository;
use DoctrineORMMapping as ORM;
#[ORMEntity(repositoryClass: ProfileTagRepository::class)]
#[ORMTable(name: '`profile_tag`')]
class ProfileTag
{
#[ORMId]
#[ORMManyToOne(targetEntity: Profile::class, inversedBy: 'profileTag')]
#[ORMJoinColumn(nullable: false)]
private ?Profile $profile = null;
#[ORMId]
#[ORMManyToOne(targetEntity: Tag::class, inversedBy: 'profileTag')]
#[ORMJoinColumn(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;
}
}
?>
And I attempt to remove in my controller as follows:
<?php
$tags = $profile->getTag();
foreach ($tags as $tag) {
$this->entityManager->remove($tag);
}
$this->entityManager->flush();
?>
How can I remove tag safely, including it’s entry in the junction table?