feat: update database relation

This commit is contained in:
Maël Gangloff
2024-07-11 18:29:22 +02:00
parent 270ffc5cb9
commit bad27c7b42
6 changed files with 96 additions and 92 deletions

View File

@@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration;
/** /**
* Auto-generated Migration: Please modify to your needs! * Auto-generated Migration: Please modify to your needs!
*/ */
final class Version20240711145906 extends AbstractMigration final class Version20240711162405 extends AbstractMigration
{ {
public function getDescription(): string public function getDescription(): string
{ {
@@ -32,6 +32,9 @@ final class Version20240711145906 extends AbstractMigration
, PRIMARY KEY("action", domain_id), CONSTRAINT FK_E8D52271115F0EE5 FOREIGN KEY (domain_id) REFERENCES domain (handle) NOT DEFERRABLE INITIALLY IMMEDIATE)'); , PRIMARY KEY("action", domain_id), CONSTRAINT FK_E8D52271115F0EE5 FOREIGN KEY (domain_id) REFERENCES domain (handle) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_E8D52271115F0EE5 ON domain_event (domain_id)'); $this->addSql('CREATE INDEX IDX_E8D52271115F0EE5 ON domain_event (domain_id)');
$this->addSql('CREATE TABLE entity (handle VARCHAR(255) NOT NULL, PRIMARY KEY(handle))'); $this->addSql('CREATE TABLE entity (handle VARCHAR(255) NOT NULL, PRIMARY KEY(handle))');
$this->addSql('CREATE TABLE event (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, entity_id VARCHAR(255) NOT NULL, "action" VARCHAR(255) NOT NULL, date DATETIME NOT NULL --(DC2Type:datetime_immutable)
, CONSTRAINT FK_3BAE0AA781257D5D FOREIGN KEY (entity_id) REFERENCES entity (handle) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_3BAE0AA781257D5D ON event (entity_id)');
$this->addSql('CREATE TABLE nameserver (handle VARCHAR(255) NOT NULL, ldh_name VARCHAR(255) NOT NULL, status CLOB NOT NULL --(DC2Type:simple_array) $this->addSql('CREATE TABLE nameserver (handle VARCHAR(255) NOT NULL, ldh_name VARCHAR(255) NOT NULL, status CLOB NOT NULL --(DC2Type:simple_array)
, PRIMARY KEY(handle))'); , PRIMARY KEY(handle))');
$this->addSql('CREATE TABLE nameserver_entity (nameserver_id VARCHAR(255) NOT NULL, entity_id VARCHAR(255) NOT NULL, roles CLOB NOT NULL --(DC2Type:simple_array) $this->addSql('CREATE TABLE nameserver_entity (nameserver_id VARCHAR(255) NOT NULL, entity_id VARCHAR(255) NOT NULL, roles CLOB NOT NULL --(DC2Type:simple_array)
@@ -39,9 +42,6 @@ final class Version20240711145906 extends AbstractMigration
, PRIMARY KEY(nameserver_id, entity_id), CONSTRAINT FK_A269AFB41A555619 FOREIGN KEY (nameserver_id) REFERENCES nameserver (handle) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_A269AFB481257D5D FOREIGN KEY (entity_id) REFERENCES entity (handle) NOT DEFERRABLE INITIALLY IMMEDIATE)'); , PRIMARY KEY(nameserver_id, entity_id), CONSTRAINT FK_A269AFB41A555619 FOREIGN KEY (nameserver_id) REFERENCES nameserver (handle) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_A269AFB481257D5D FOREIGN KEY (entity_id) REFERENCES entity (handle) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_A269AFB41A555619 ON nameserver_entity (nameserver_id)'); $this->addSql('CREATE INDEX IDX_A269AFB41A555619 ON nameserver_entity (nameserver_id)');
$this->addSql('CREATE INDEX IDX_A269AFB481257D5D ON nameserver_entity (entity_id)'); $this->addSql('CREATE INDEX IDX_A269AFB481257D5D ON nameserver_entity (entity_id)');
$this->addSql('CREATE TABLE nameserver_event ("action" VARCHAR(255) NOT NULL, nameserver_id VARCHAR(255) NOT NULL, date DATETIME NOT NULL --(DC2Type:datetime_immutable)
, PRIMARY KEY(nameserver_id, "action"), CONSTRAINT FK_C6BC968C1A555619 FOREIGN KEY (nameserver_id) REFERENCES nameserver (handle) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_C6BC968C1A555619 ON nameserver_event (nameserver_id)');
$this->addSql('CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, email VARCHAR(180) NOT NULL, roles CLOB NOT NULL --(DC2Type:json) $this->addSql('CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, email VARCHAR(180) NOT NULL, roles CLOB NOT NULL --(DC2Type:json)
, password VARCHAR(255) NOT NULL)'); , password VARCHAR(255) NOT NULL)');
$this->addSql('CREATE UNIQUE INDEX UNIQ_IDENTIFIER_EMAIL ON user (email)'); $this->addSql('CREATE UNIQUE INDEX UNIQ_IDENTIFIER_EMAIL ON user (email)');
@@ -62,9 +62,9 @@ final class Version20240711145906 extends AbstractMigration
$this->addSql('DROP TABLE domain_entity'); $this->addSql('DROP TABLE domain_entity');
$this->addSql('DROP TABLE domain_event'); $this->addSql('DROP TABLE domain_event');
$this->addSql('DROP TABLE entity'); $this->addSql('DROP TABLE entity');
$this->addSql('DROP TABLE event');
$this->addSql('DROP TABLE nameserver'); $this->addSql('DROP TABLE nameserver');
$this->addSql('DROP TABLE nameserver_entity'); $this->addSql('DROP TABLE nameserver_entity');
$this->addSql('DROP TABLE nameserver_event');
$this->addSql('DROP TABLE user'); $this->addSql('DROP TABLE user');
$this->addSql('DROP TABLE messenger_messages'); $this->addSql('DROP TABLE messenger_messages');
} }

View File

@@ -1,8 +0,0 @@
<?php
namespace App\Controller;
class TestController
{
}

View File

@@ -5,6 +5,7 @@ namespace App\Entity;
use App\Repository\EntityRepository; use App\Repository\EntityRepository;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: EntityRepository::class)] #[ORM\Entity(repositoryClass: EntityRepository::class)]
@@ -26,10 +27,17 @@ class Entity
#[ORM\OneToMany(targetEntity: NameserverEntity::class, mappedBy: 'entity')] #[ORM\OneToMany(targetEntity: NameserverEntity::class, mappedBy: 'entity')]
private Collection $nameserverEntities; private Collection $nameserverEntities;
/**
* @var Collection<int, Event>
*/
#[ORM\OneToMany(targetEntity: Event::class, mappedBy: 'entity', orphanRemoval: true)]
private Collection $events;
public function __construct() public function __construct()
{ {
$this->domainEntities = new ArrayCollection(); $this->domainEntities = new ArrayCollection();
$this->nameserverEntities = new ArrayCollection(); $this->nameserverEntities = new ArrayCollection();
$this->events = new ArrayCollection();
} }
public function getHandle(): ?string public function getHandle(): ?string
@@ -103,4 +111,35 @@ class Entity
return $this; return $this;
} }
/**
* @return Collection<int, Event>
*/
public function getEvents(): Collection
{
return $this->events;
}
public function addEvent(Event $event): static
{
if (!$this->events->contains($event)) {
$this->events->add($event);
$event->setEntity($this);
}
return $this;
}
public function removeEvent(Event $event): static
{
if ($this->events->removeElement($event)) {
// set the owning side to null (unless already changed)
if ($event->getEntity() === $this) {
$event->setEntity(null);
}
}
return $this;
}
} }

View File

@@ -3,46 +3,31 @@
namespace App\Entity; namespace App\Entity;
use App\Config\EventAction; use App\Config\EventAction;
use App\Repository\NameserverEventRepository; use App\Repository\EventRepository;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: NameserverEventRepository::class)] #[ORM\Entity(repositoryClass: EventRepository::class)]
class NameserverEvent class Event
{ {
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column] #[ORM\Column]
private ?\DateTimeImmutable $date = null; private ?int $id = null;
#[ORM\Id]
#[ORM\ManyToOne(inversedBy: 'nameserverEvents')]
#[ORM\JoinColumn(referencedColumnName: 'handle', nullable: false)]
private ?Nameserver $nameserver = null;
#[ORM\Id]
#[ORM\Column(enumType: EventAction::class)] #[ORM\Column(enumType: EventAction::class)]
private ?EventAction $action = null; private ?EventAction $action = null;
public function getDate(): ?\DateTimeImmutable #[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
{ {
return $this->date; return $this->id;
}
public function setDate(\DateTimeImmutable $date): static
{
$this->date = $date;
return $this;
}
public function getNameserver(): ?Nameserver
{
return $this->nameserver;
}
public function setNameserver(?Nameserver $nameserver): static
{
$this->nameserver = $nameserver;
return $this;
} }
public function getAction(): ?EventAction public function getAction(): ?EventAction
@@ -56,4 +41,29 @@ class NameserverEvent
return $this; return $this;
} }
public function getDate(): ?\DateTimeImmutable
{
return $this->date;
}
public function setDate(\DateTimeImmutable $date): static
{
$this->date = $date;
return $this;
}
public function getEntity(): ?Entity
{
return $this->entity;
}
public function setEntity(?Entity $entity): static
{
$this->entity = $entity;
return $this;
}
} }

View File

@@ -28,16 +28,9 @@ class Nameserver
#[ORM\Column(type: Types::SIMPLE_ARRAY, enumType: DomainStatus::class)] #[ORM\Column(type: Types::SIMPLE_ARRAY, enumType: DomainStatus::class)]
private array $status = []; private array $status = [];
/**
* @var Collection<int, NameserverEvent>
*/
#[ORM\OneToMany(targetEntity: NameserverEvent::class, mappedBy: 'nameserver', orphanRemoval: true)]
private Collection $nameserverEvents;
public function __construct() public function __construct()
{ {
$this->nameserverEntities = new ArrayCollection(); $this->nameserverEntities = new ArrayCollection();
$this->nameserverEvents = new ArrayCollection();
} }
public function getHandle(): ?string public function getHandle(): ?string
@@ -109,34 +102,4 @@ class Nameserver
return $this; return $this;
} }
/**
* @return Collection<int, NameserverEvent>
*/
public function getNameserverEvents(): Collection
{
return $this->nameserverEvents;
}
public function addNameserverEvent(NameserverEvent $nameserverEvent): static
{
if (!$this->nameserverEvents->contains($nameserverEvent)) {
$this->nameserverEvents->add($nameserverEvent);
$nameserverEvent->setNameserver($this);
}
return $this;
}
public function removeNameserverEvent(NameserverEvent $nameserverEvent): static
{
if ($this->nameserverEvents->removeElement($nameserverEvent)) {
// set the owning side to null (unless already changed)
if ($nameserverEvent->getNameserver() === $this) {
$nameserverEvent->setNameserver(null);
}
}
return $this;
}
} }

View File

@@ -2,39 +2,39 @@
namespace App\Repository; namespace App\Repository;
use App\Entity\NameserverEvent; use App\Entity\Event;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
/** /**
* @extends ServiceEntityRepository<NameserverEvent> * @extends ServiceEntityRepository<Event>
*/ */
class NameserverEventRepository extends ServiceEntityRepository class EventRepository extends ServiceEntityRepository
{ {
public function __construct(ManagerRegistry $registry) public function __construct(ManagerRegistry $registry)
{ {
parent::__construct($registry, NameserverEvent::class); parent::__construct($registry, Event::class);
} }
// /** // /**
// * @return NameserverEvent[] Returns an array of NameserverEvent objects // * @return Event[] Returns an array of Event objects
// */ // */
// public function findByExampleField($value): array // public function findByExampleField($value): array
// { // {
// return $this->createQueryBuilder('n') // return $this->createQueryBuilder('e')
// ->andWhere('n.exampleField = :val') // ->andWhere('e.exampleField = :val')
// ->setParameter('val', $value) // ->setParameter('val', $value)
// ->orderBy('n.id', 'ASC') // ->orderBy('e.id', 'ASC')
// ->setMaxResults(10) // ->setMaxResults(10)
// ->getQuery() // ->getQuery()
// ->getResult() // ->getResult()
// ; // ;
// } // }
// public function findOneBySomeField($value): ?NameserverEvent // public function findOneBySomeField($value): ?Event
// { // {
// return $this->createQueryBuilder('n') // return $this->createQueryBuilder('e')
// ->andWhere('n.exampleField = :val') // ->andWhere('e.exampleField = :val')
// ->setParameter('val', $value) // ->setParameter('val', $value)
// ->getQuery() // ->getQuery()
// ->getOneOrNullResult() // ->getOneOrNullResult()