Implement snip tags with very elegant tags form

This commit is contained in:
Tim
2025-05-10 20:06:16 +02:00
parent 47ea226ed7
commit e2bd1a7c3b
7 changed files with 325 additions and 8 deletions

View File

@@ -39,9 +39,16 @@ class Snip
#[ORM\Column]
private bool $archived = false;
/**
* @var Collection<int, Tag>
*/
#[ORM\ManyToMany(targetEntity: Tag::class, mappedBy: 'snips')]
private Collection $tags;
public function __construct()
{
$this->snipContents = new ArrayCollection();
$this->tags = new ArrayCollection();
}
public function __toString(): string
@@ -160,4 +167,31 @@ class Snip
return $this;
}
/**
* @return Collection<int, Tag>
*/
public function getTags(): Collection
{
return $this->tags;
}
public function addTag(Tag $tag): static
{
if (!$this->tags->contains($tag)) {
$this->tags->add($tag);
$tag->addSnip($this);
}
return $this;
}
public function removeTag(Tag $tag): static
{
if ($this->tags->removeElement($tag)) {
$tag->removeSnip($this);
}
return $this;
}
}

94
src/Entity/Tag.php Normal file
View File

@@ -0,0 +1,94 @@
<?php
namespace App\Entity;
use App\Repository\TagRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: TagRepository::class)]
#[ORM\UniqueConstraint(name: 'user_tag_unique', columns: ['name', 'user_id'])]
class Tag
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
/**
* @var Collection<int, Snip>
*/
#[ORM\ManyToMany(targetEntity: Snip::class, inversedBy: 'tags')]
private Collection $snips;
public function __construct()
{
$this->snips = new ArrayCollection();
}
public function __toString(): string
{
return $this->name ?? '';
}
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 getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
/**
* @return Collection<int, Snip>
*/
public function getSnips(): Collection
{
return $this->snips;
}
public function addSnip(Snip $snip): static
{
if (!$this->snips->contains($snip)) {
$this->snips->add($snip);
}
return $this;
}
public function removeSnip(Snip $snip): static
{
$this->snips->removeElement($snip);
return $this;
}
}