feat: update database relation

This commit is contained in:
Maël Gangloff
2024-07-11 22:20:20 +02:00
parent bad27c7b42
commit 6aad09297f
11 changed files with 146 additions and 83 deletions

View File

@@ -2,14 +2,14 @@
namespace App\Entity;
use App\Repository\BookmarkDomainListRepository;
use App\Repository\BookmarkListRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
#[ORM\Entity(repositoryClass: BookmarkDomainListRepository::class)]
class BookmarkDomainList
#[ORM\Entity(repositoryClass: BookmarkListRepository::class)]
class BookmarkList
{
#[ORM\Id]
#[ORM\Column(length: 36)]
@@ -22,7 +22,10 @@ class BookmarkDomainList
/**
* @var Collection<int, Domain>
*/
#[ORM\ManyToMany(targetEntity: Domain::class, mappedBy: 'handle')]
#[ORM\ManyToMany(targetEntity: Domain::class, inversedBy: 'bookmarkLists')]
#[ORM\JoinTable(name: 'bookmark_lists_domains',
joinColumns: [new ORM\JoinColumn(name: 'bookmark_token', referencedColumnName: 'token')],
inverseJoinColumns: [new ORM\JoinColumn(name: 'domain_ldh_name', referencedColumnName: 'ldh_name'), new ORM\JoinColumn(name: 'domain_handle', referencedColumnName: 'handle')])]
private Collection $domains;
public function __construct()

View File

@@ -38,16 +38,27 @@ class Domain
#[ORM\Column(type: Types::SIMPLE_ARRAY, enumType: DomainStatus::class)]
private array $status = [];
/**
* @var Collection<int, BookmarkList>
*/
#[ORM\ManyToMany(targetEntity: BookmarkList::class, mappedBy: 'domains')]
private Collection $bookmarkLists;
/**
* @var Collection<int, Nameserver>
*/
#[ORM\ManyToMany(targetEntity: Nameserver::class, mappedBy: 'handle')]
#[ORM\ManyToMany(targetEntity: Nameserver::class, inversedBy: 'domains')]
#[ORM\JoinTable(name: 'domain_nameservers',
joinColumns: [new ORM\JoinColumn(name: 'domain_handle', referencedColumnName: 'handle'), new ORM\JoinColumn(name: 'domain_ldh_name', referencedColumnName: 'ldh_name')],
inverseJoinColumns: [new ORM\JoinColumn(name: 'nameserver_handle', referencedColumnName: 'handle')]
)]
private Collection $nameservers;
public function __construct()
{
$this->events = new ArrayCollection();
$this->domainEntities = new ArrayCollection();
$this->bookmarkLists = new ArrayCollection();
$this->nameservers = new ArrayCollection();
}
@@ -162,6 +173,33 @@ class Domain
return $this;
}
/**
* @return Collection<int, BookmarkList>
*/
public function getBookmarkLists(): Collection
{
return $this->bookmarkLists;
}
public function addBookmarkList(BookmarkList $bookmarkList): static
{
if (!$this->bookmarkLists->contains($bookmarkList)) {
$this->bookmarkLists->add($bookmarkList);
$bookmarkList->addDomain($this);
}
return $this;
}
public function removeBookmarkList(BookmarkList $bookmarkList): static
{
if ($this->bookmarkLists->removeElement($bookmarkList)) {
$bookmarkList->removeDomain($this);
}
return $this;
}
/**
* @return Collection<int, Nameserver>
*/

View File

@@ -2,50 +2,16 @@
namespace App\Entity;
use App\Config\EventAction;
use App\Repository\DomainEventRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: DomainEventRepository::class)]
class DomainEvent
class DomainEvent extends Event
{
#[ORM\Id]
#[ORM\Column(enumType: EventAction::class)]
private ?EventAction $action = null;
#[ORM\Column(type: Types::DATE_IMMUTABLE)]
private ?\DateTimeImmutable $date = null;
#[ORM\Id]
#[ORM\ManyToOne(targetEntity: Domain::class, inversedBy: 'events')]
#[ORM\JoinColumn(referencedColumnName: 'handle', nullable: false)]
private ?Domain $domain = null;
public function getAction(): ?EventAction
{
return $this->action;
}
public function setAction(EventAction $action): static
{
$this->action = $action;
return $this;
}
public function getDate(): ?\DateTimeImmutable
{
return $this->date;
}
public function setDate(\DateTimeImmutable $date): static
{
$this->date = $date;
return $this;
}
public function getDomain(): ?Domain
{
return $this->domain;

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Entity;
use App\Config\EventAction;
use App\Repository\EntityEventRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: EntityEventRepository::class)]
class EntityEvent extends Event
{
#[ORM\ManyToOne(targetEntity: Entity::class, inversedBy: 'events')]
#[ORM\JoinColumn(referencedColumnName: 'handle', nullable: false)]
private ?Entity $entity = null;
public function getEntity(): ?Entity
{
return $this->entity;
}
public function setEntity(?Entity $entity): static
{
$this->entity = $entity;
return $this;
}
}

View File

@@ -3,10 +3,10 @@
namespace App\Entity;
use App\Config\EventAction;
use App\Repository\EventRepository;
use App\Repository\EntityEventRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: EventRepository::class)]
#[ORM\MappedSuperclass]
class Event
{
#[ORM\Id]
@@ -20,10 +20,6 @@ class Event
#[ORM\Column]
private ?\DateTimeImmutable $date = null;
#[ORM\ManyToOne(targetEntity: Entity::class, inversedBy: 'events')]
#[ORM\JoinColumn(referencedColumnName: 'handle', nullable: false)]
private ?Entity $entity = null;
public function getId(): ?int
{
@@ -54,16 +50,4 @@ class Event
return $this;
}
public function getEntity(): ?Entity
{
return $this->entity;
}
public function setEntity(?Entity $entity): static
{
$this->entity = $entity;
return $this;
}
}

View File

@@ -28,9 +28,16 @@ class Nameserver
#[ORM\Column(type: Types::SIMPLE_ARRAY, enumType: DomainStatus::class)]
private array $status = [];
/**
* @var Collection<int, Domain>
*/
#[ORM\ManyToMany(targetEntity: Domain::class, mappedBy: 'nameservers')]
private Collection $domains;
public function __construct()
{
$this->nameserverEntities = new ArrayCollection();
$this->domains = new ArrayCollection();
}
public function getHandle(): ?string
@@ -102,4 +109,31 @@ class Nameserver
return $this;
}
/**
* @return Collection<int, Domain>
*/
public function getDomains(): Collection
{
return $this->domains;
}
public function addDomain(Domain $domain): static
{
if (!$this->domains->contains($domain)) {
$this->domains->add($domain);
$domain->addNameserver($this);
}
return $this;
}
public function removeDomain(Domain $domain): static
{
if ($this->domains->removeElement($domain)) {
$domain->removeNameserver($this);
}
return $this;
}
}

View File

@@ -34,9 +34,9 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
private ?string $password = null;
/**
* @var Collection<int, BookmarkDomainList>
* @var Collection<int, BookmarkList>
*/
#[ORM\OneToMany(targetEntity: BookmarkDomainList::class, mappedBy: 'user', orphanRemoval: true)]
#[ORM\OneToMany(targetEntity: BookmarkList::class, mappedBy: 'user', orphanRemoval: true)]
private Collection $bookmarkDomainLists;
public function __construct()
@@ -121,14 +121,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
}
/**
* @return Collection<int, BookmarkDomainList>
* @return Collection<int, BookmarkList>
*/
public function getBookmarkDomainLists(): Collection
{
return $this->bookmarkDomainLists;
}
public function addBookmarkDomainList(BookmarkDomainList $bookmarkDomainList): static
public function addBookmarkDomainList(BookmarkList $bookmarkDomainList): static
{
if (!$this->bookmarkDomainLists->contains($bookmarkDomainList)) {
$this->bookmarkDomainLists->add($bookmarkDomainList);
@@ -138,7 +138,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
return $this;
}
public function removeBookmarkDomainList(BookmarkDomainList $bookmarkDomainList): static
public function removeBookmarkDomainList(BookmarkList $bookmarkDomainList): static
{
if ($this->bookmarkDomainLists->removeElement($bookmarkDomainList)) {
// set the owning side to null (unless already changed)