Fix Document ↔ Tag relation, add parent to Tag entity

+ Create migrations
master
Julien Rosset 1 year ago
parent 337aa5846f
commit 0e494bb3f1

@ -0,0 +1,77 @@
<?php
declare(strict_types = 1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240429144740 extends AbstractMigration {
public function getDescription (): string {
return '';
}
public function up (Schema $schema): void {
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE ext_log_entries (id INT AUTO_INCREMENT NOT NULL, action VARCHAR(8) NOT NULL, logged_at DATETIME NOT NULL, object_id VARCHAR(64) DEFAULT NULL, object_class VARCHAR(191) NOT NULL, version INT NOT NULL, data LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:array)\', username VARCHAR(191) DEFAULT NULL, INDEX log_class_lookup_idx (object_class), INDEX log_date_lookup_idx (logged_at), INDEX log_user_lookup_idx (username), INDEX log_version_lookup_idx (object_id, object_class, version), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC'
);
$this->addSql('CREATE TABLE ext_translations (id int AUTO_INCREMENT NOT NULL, locale varchar(8) NOT NULL, object_class varchar(191) NOT NULL, field varchar(32) NOT NULL, foreign_key varchar(64) NOT NULL, content longtext DEFAULT NULL, INDEX translations_lookup_idx (locale, object_class, foreign_key), INDEX general_translations_lookup_idx (object_class, foreign_key), UNIQUE INDEX lookup_unique_idx (locale, object_class, field, foreign_key), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC'
);
$this->addSql('CREATE TABLE tag_document (tag_id int NOT NULL, document_id int NOT NULL, INDEX IDX_EE58F1ADBAD26311 (tag_id), INDEX IDX_EE58F1ADC33F7837 (document_id), PRIMARY KEY(tag_id, document_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'
);
$this->addSql('ALTER TABLE tag_document ADD CONSTRAINT FK_EE58F1ADBAD26311 FOREIGN KEY (tag_id) REFERENCES tag (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE tag_document ADD CONSTRAINT FK_EE58F1ADC33F7837 FOREIGN KEY (document_id) REFERENCES document (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE document DROP FOREIGN KEY FK_D8698A76A76ED395');
$this->addSql('DROP INDEX IDX_D8698A76A76ED395 ON document');
$this->addSql('ALTER TABLE document ADD file_name VARCHAR(255) NOT NULL, ADD file_type VARCHAR(255) NOT NULL, ADD file_size NUMERIC(10, 0) NOT NULL, CHANGE user_id owner_id INT NOT NULL, CHANGE name file_path VARCHAR(255) NOT NULL, CHANGE modification last_modification DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\''
);
$this->addSql('ALTER TABLE document ADD CONSTRAINT FK_D8698A767E3C61F9 FOREIGN KEY (owner_id) REFERENCES user (id)');
$this->addSql('CREATE INDEX IDX_D8698A767E3C61F9 ON document (owner_id)');
$this->addSql('ALTER TABLE tag ADD owner_id INT NOT NULL, ADD parent_id INT DEFAULT NULL, ADD path LONGTEXT NOT NULL, ADD creation DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', ADD last_modification DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\''
);
$this->addSql('ALTER TABLE tag ADD CONSTRAINT FK_389B7837E3C61F9 FOREIGN KEY (owner_id) REFERENCES user (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE tag ADD CONSTRAINT FK_389B783727ACA70 FOREIGN KEY (parent_id) REFERENCES tag (id) ON DELETE CASCADE');
$this->addSql('CREATE INDEX IDX_389B7837E3C61F9 ON tag (owner_id)');
$this->addSql('CREATE INDEX IDX_389B783727ACA70 ON tag (parent_id)');
$this->addSql('DROP INDEX UNIQ_8D93D649F85E0677 ON user');
$this->addSql('ALTER TABLE user ADD validation_administrator_id INT DEFAULT NULL, ADD email VARCHAR(100) NOT NULL, ADD name VARCHAR(100) DEFAULT NULL, ADD validation_date DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', ADD creation DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', ADD last_modification DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', DROP username, CHANGE roles roles JSON NOT NULL COMMENT \'(DC2Type:json)\''
);
$this->addSql('ALTER TABLE user ADD CONSTRAINT FK_8D93D6495F4CC0C2 FOREIGN KEY (validation_administrator_id) REFERENCES user (id)');
$this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649E7927C74 ON user (email)');
$this->addSql('CREATE INDEX IDX_8D93D6495F4CC0C2 ON user (validation_administrator_id)');
$this->addSql('ALTER TABLE messenger_messages CHANGE created_at created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', CHANGE available_at available_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', CHANGE delivered_at delivered_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\''
);
}
public function down (Schema $schema): void {
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE tag_document DROP FOREIGN KEY FK_EE58F1ADBAD26311');
$this->addSql('ALTER TABLE tag_document DROP FOREIGN KEY FK_EE58F1ADC33F7837');
$this->addSql('DROP TABLE ext_log_entries');
$this->addSql('DROP TABLE ext_translations');
$this->addSql('DROP TABLE tag_document');
$this->addSql('ALTER TABLE document DROP FOREIGN KEY FK_D8698A767E3C61F9');
$this->addSql('DROP INDEX IDX_D8698A767E3C61F9 ON document');
$this->addSql('ALTER TABLE document ADD name VARCHAR(255) NOT NULL, DROP file_path, DROP file_name, DROP file_type, DROP file_size, CHANGE owner_id user_id INT NOT NULL, CHANGE last_modification modification DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\''
);
$this->addSql('ALTER TABLE document ADD CONSTRAINT FK_D8698A76A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
$this->addSql('CREATE INDEX IDX_D8698A76A76ED395 ON document (user_id)');
$this->addSql('ALTER TABLE messenger_messages CHANGE created_at created_at datetime NOT NULL, CHANGE available_at available_at datetime NOT NULL, CHANGE delivered_at delivered_at datetime DEFAULT NULL'
);
$this->addSql('ALTER TABLE tag DROP FOREIGN KEY FK_389B7837E3C61F9');
$this->addSql('ALTER TABLE tag DROP FOREIGN KEY FK_389B783727ACA70');
$this->addSql('DROP INDEX IDX_389B7837E3C61F9 ON tag');
$this->addSql('DROP INDEX IDX_389B783727ACA70 ON tag');
$this->addSql('ALTER TABLE tag DROP owner_id, DROP parent_id, DROP path, DROP creation, DROP last_modification');
$this->addSql('ALTER TABLE user DROP FOREIGN KEY FK_8D93D6495F4CC0C2');
$this->addSql('DROP INDEX UNIQ_8D93D649E7927C74 ON user');
$this->addSql('DROP INDEX IDX_8D93D6495F4CC0C2 ON user');
$this->addSql('ALTER TABLE user ADD username VARCHAR(180) NOT NULL, DROP validation_administrator_id, DROP email, DROP name, DROP validation_date, DROP creation, DROP last_modification, CHANGE roles roles JSON NOT NULL COMMENT \'(DC2Type:json)\''
);
$this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649F85E0677 ON user (username)');
}
}

@ -0,0 +1,32 @@
<?php
declare(strict_types = 1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240429145559 extends AbstractMigration {
public function getDescription (): string {
return '';
}
public function up (Schema $schema): void {
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE tag_document DROP FOREIGN KEY FK_EE58F1ADC33F7837');
$this->addSql('ALTER TABLE tag_document DROP FOREIGN KEY FK_EE58F1ADBAD26311');
$this->addSql('DROP TABLE tag_document');
}
public function down (Schema $schema): void {
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE tag_document (tag_id int NOT NULL, document_id int NOT NULL, INDEX IDX_EE58F1ADC33F7837 (document_id), INDEX IDX_EE58F1ADBAD26311 (tag_id), PRIMARY KEY(tag_id, document_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB COMMENT = \'\' '
);
$this->addSql('ALTER TABLE tag_document ADD CONSTRAINT FK_EE58F1ADC33F7837 FOREIGN KEY (document_id) REFERENCES document (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE tag_document ADD CONSTRAINT FK_EE58F1ADBAD26311 FOREIGN KEY (tag_id) REFERENCES tag (id) ON DELETE CASCADE');
}
}

@ -210,11 +210,11 @@ class Document {
return $this->tags;
}
/**
* Add a tag
* Add a {@see Tag tag}
*
* Do nothing if the tag is already present
*
* @param Tag $tag The tag to add
* @param Tag $tag The {@see Tag tag} to add
*
* @return $this
*/
@ -225,9 +225,9 @@ class Document {
return $this;
}
/**
* Remove a tag
* Remove a {@see Tag tag}
*
* @param Tag $tag The tag to remove
* @param Tag $tag The {@see Tag tag} to remove
*
* @return $this
*/

@ -4,6 +4,7 @@ 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;
@ -22,42 +23,62 @@ class Tag {
use TEntityBase;
/**
* @var User|null The owner
* @var User The owner
*/
#[ORM\ManyToOne(inversedBy: 'tags')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
#[Assert\Valid]
private ?User $owner = null;
private User $owner;
/**
* @var string|null The name
* @var string The name
*/
#[ORM\Column(length: 50)]
#[Gedmo\TreePathSource]
#[Assert\NotBlank]
private ?string $name = null;
private string $name;
/**
* @var string|null The path (complete name)
* @var string The path (complete name)
*/
#[ORM\Column(type: Types::TEXT)]
#[Gedmo\TreePath]
#[Assert\NotBlank]
private ?string $path = null;
private string $path;
/**
* @var Tag|null The parent tag
*/
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
#[ORM\JoinColumn(onDelete: 'CASCADE')]
#[Gedmo\TreeParent]
private ?self $parent = null;
/**
* @var Collection<int, self> The children tag
*/
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
private Collection $children;
/**
* @var Collection<int, Document> The {@see Document documents}
*/
#[ORM\ManyToMany(targetEntity: Document::class, mappedBy: 'tags')]
private Collection $documents;
/**
* Initialization
*/
public function __construct () {
$this->children = new ArrayCollection();
$this->documents = new ArrayCollection();
}
/**
* The owner
*
* @return User|null The owner
* @return User The owner
*/
public function getOwner (): ?User {
public function getOwner (): User {
return $this->owner;
}
/**
@ -76,9 +97,9 @@ class Tag {
/**
* The name
*
* @return string|null The name
* @return string The name
*/
public function getName (): ?string {
public function getName (): string {
return $this->name;
}
/**
@ -97,9 +118,9 @@ class Tag {
/**
* The path (complete name)
*
* @return string|null The path (complete name)
* @return string The path (complete name)
*/
public function getPath (): ?string
public function getPath (): string
{
return $this->path;
}
@ -116,4 +137,73 @@ class Tag {
return $this;
}
/**
* The parent tag
*
* @return Tag|null The parent tag
*/
public function getParent (): ?Tag {
return $this->parent;
}
/**
* Change the parent tag
*
* @param Tag|null $parent The new parent tag
*
* @return $this
*/
public function setParent (?Tag $parent): self {
$this->parent = $parent;
return $this;
}
/**
* The children tag
*
* @return Collection<int, self> The children tag
*/
public function getChildren (): Collection {
return $this->children;
}
/**
* The {@see Document documents}
*
* @return Collection<int, Document> The {@see Document documents}
*/
public function getDocuments (): Collection {
return $this->documents;
}
/**
* Add a {@see Document document}
*
* Do nothing if the document is already present
*
* @param Document $document The {@see Document document} to add
*
* @return $this
*/
public function addDocument (Document $document): static {
if (!$this->documents->contains($document)) {
$this->documents->add($document);
$document->addTag($this);
}
return $this;
}
/**
* Remove a {@see Document document}
*
* @param Document $document The {@see Document document} to remove
*
* @return $this
*/
public function removeDocument (Document $document): static {
if ($this->documents->removeElement($document)) {
$document->removeTag($this);
}
return $this;
}
}

Loading…
Cancel
Save