Update Tag entity

master
Julien Rosset 1 year ago
parent 6b19b56cd0
commit 337aa5846f

@ -4,68 +4,115 @@ namespace App\Entity;
use App\Repository\TagRepository; use App\Repository\TagRepository;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM; 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)] #[ORM\Entity(repositoryClass: TagRepository::class)]
class Tag #[Gedmo\Tree(type: 'materializedPath')]
{ class Tag {
#[ORM\Id] use TEntityBase;
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
/**
* @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)] #[ORM\Column(length: 50)]
#[Gedmo\TreePathSource]
#[Assert\NotBlank]
private ?string $name = null; 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(); $this->documents = new ArrayCollection();
} }
public function getId(): ?int /**
{ * The owner
return $this->id; *
* @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;
{
return $this->name;
} }
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; $this->name = $name;
return $this; return $this;
} }
/** /**
* @return Collection<int, Document> * 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 * Change the path (complete name)
{ *
if (!$this->documents->contains($document)) { * @param string $path The new path (complete name)
$this->documents->add($document); *
$document->addTag($this); * @return $this
} */
public function setPath (string $path): static
return $this;
}
public function removeDocument(Document $document): self
{ {
if ($this->documents->removeElement($document)) { $this->path = $path;
$document->removeTag($this);
}
return $this; return $this;
} }

@ -197,7 +197,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface {
public function addTag (Tag $tag): static { public function addTag (Tag $tag): static {
if (!$this->tags->contains($tag)) { if (!$this->tags->contains($tag)) {
$this->tags->add($tag); $this->tags->add($tag);
$tag->setUser($this); $tag->setOwner($this);
} }
return $this; return $this;
@ -213,8 +213,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface {
*/ */
public function removeTag (Tag $tag): static { public function removeTag (Tag $tag): static {
if ($this->tags->removeElement($tag)) { if ($this->tags->removeElement($tag)) {
if ($tag->getUser() === $this) { if ($tag->getOwner() === $this) {
$tag->setUser(null); $tag->setOwner(null);
} }
} }

Loading…
Cancel
Save