From 337aa5846f82ebcc4d8f772cd06d90da3a79858a Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Fri, 26 Apr 2024 18:29:26 +0200 Subject: [PATCH] Update Tag entity --- src/Entity/Tag.php | 121 ++++++++++++++++++++++++++++++-------------- src/Entity/User.php | 6 +-- 2 files changed, 87 insertions(+), 40 deletions(-) diff --git a/src/Entity/Tag.php b/src/Entity/Tag.php index 37e6b8d..1d5995a 100644 --- a/src/Entity/Tag.php +++ b/src/Entity/Tag.php @@ -4,68 +4,115 @@ namespace App\Entity; use App\Repository\TagRepository; use Doctrine\Common\Collections\ArrayCollection; -use Doctrine\Common\Collections\Collection; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; +use Gedmo\Mapping\Annotation as Gedmo; +use Symfony\Component\Validator\Constraints as Assert; +/** + * A tag + * + * Tags are by user and a “materialized path” tree structure + * + * @link https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/tree.md#materialized-path + */ #[ORM\Entity(repositoryClass: TagRepository::class)] -class Tag -{ - #[ORM\Id] - #[ORM\GeneratedValue] - #[ORM\Column] - private ?int $id = null; +#[Gedmo\Tree(type: 'materializedPath')] +class Tag { + use TEntityBase; + /** + * @var User|null The owner + */ + #[ORM\ManyToOne(inversedBy: 'tags')] + #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] + #[Assert\Valid] + private ?User $owner = null; + + /** + * @var string|null The name + */ #[ORM\Column(length: 50)] + #[Gedmo\TreePathSource] + #[Assert\NotBlank] private ?string $name = null; - #[ORM\ManyToMany(targetEntity: Document::class, mappedBy: 'tags')] - private Collection $documents; + /** + * @var string|null The path (complete name) + */ + #[ORM\Column(type: Types::TEXT)] + #[Gedmo\TreePath] + #[Assert\NotBlank] + private ?string $path = null; - public function __construct() - { + /** + * Initialization + */ + public function __construct () { $this->documents = new ArrayCollection(); } - public function getId(): ?int - { - return $this->id; + /** + * The owner + * + * @return User|null The owner + */ + public function getOwner (): ?User { + return $this->owner; } + /** + * Change the owner + * + * @param User $owner The new owner + * + * @return $this + */ + public function setOwner (User $owner): static { + $this->owner = $owner; - public function getName(): ?string - { - return $this->name; + return $this; } - public function setName(string $name): self - { + /** + * The name + * + * @return string|null The name + */ + public function getName (): ?string { + return $this->name; + } + /** + * Change the name + * + * @param string $name The new name + * + * @return $this + */ + public function setName (string $name): self { $this->name = $name; return $this; } /** - * @return Collection + * The path (complete name) + * + * @return string|null The path (complete name) */ - public function getDocuments(): Collection + public function getPath (): ?string { - return $this->documents; + return $this->path; } - - public function addDocument(Document $document): self - { - if (!$this->documents->contains($document)) { - $this->documents->add($document); - $document->addTag($this); - } - - return $this; - } - - public function removeDocument(Document $document): self + /** + * Change the path (complete name) + * + * @param string $path The new path (complete name) + * + * @return $this + */ + public function setPath (string $path): static { - if ($this->documents->removeElement($document)) { - $document->removeTag($this); - } + $this->path = $path; return $this; } diff --git a/src/Entity/User.php b/src/Entity/User.php index e532462..0c93131 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -197,7 +197,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface { public function addTag (Tag $tag): static { if (!$this->tags->contains($tag)) { $this->tags->add($tag); - $tag->setUser($this); + $tag->setOwner($this); } return $this; @@ -213,8 +213,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface { */ public function removeTag (Tag $tag): static { if ($this->tags->removeElement($tag)) { - if ($tag->getUser() === $this) { - $tag->setUser(null); + if ($tag->getOwner() === $this) { + $tag->setOwner(null); } }