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 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;
}
public function getName(): ?string
{
return $this->name;
/**
* The owner
*
* @return User|null The owner
*/
public function getOwner (): ?User {
return $this->owner;
}
public function setName(string $name): self
{
$this->name = $name;
/**
* Change the owner
*
* @param User $owner The new owner
*
* @return $this
*/
public function setOwner (User $owner): static {
$this->owner = $owner;
return $this;
}
/**
* @return Collection<int, Document>
* The name
*
* @return string|null The name
*/
public function getDocuments(): Collection
{
return $this->documents;
}
public function addDocument(Document $document): self
{
if (!$this->documents->contains($document)) {
$this->documents->add($document);
$document->addTag($this);
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;
}
public function removeDocument(Document $document): self
/**
* The path (complete name)
*
* @return string|null The path (complete name)
*/
public function getPath (): ?string
{
if ($this->documents->removeElement($document)) {
$document->removeTag($this);
return $this->path;
}
/**
* Change the path (complete name)
*
* @param string $path The new path (complete name)
*
* @return $this
*/
public function setPath (string $path): static
{
$this->path = $path;
return $this;
}

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

Loading…
Cancel
Save