소스 검색

On rend les slugs uniques

Sangfroid 2 달 전
부모
커밋
587c968a17
4개의 변경된 파일33개의 추가작업 그리고 83개의 파일을 삭제
  1. 31 0
      migrations/Version20241025154707.php
  2. 0 42
      src/Command/BlogSlugInitCommand.php
  3. 2 1
      src/Entity/Article.php
  4. 0 40
      src/Service/SluggerService.php

+ 31 - 0
migrations/Version20241025154707.php

@@ -0,0 +1,31 @@
+<?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 Version20241025154707 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 UNIQUE INDEX UNIQ_23A0E66989D9B62 ON article (slug)');
+    }
+
+    public function down(Schema $schema): void
+    {
+        // this down() migration is auto-generated, please modify it to your needs
+        $this->addSql('DROP INDEX UNIQ_23A0E66989D9B62 ON article');
+    }
+}

+ 0 - 42
src/Command/BlogSlugInitCommand.php

@@ -1,42 +0,0 @@
-<?php
-
-namespace App\Command;
-
-use App\Repository\ArticleRepository;
-use App\Service\SluggerService;
-use Symfony\Component\Console\Attribute\AsCommand;
-use Symfony\Component\Console\Command\Command;
-use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Console\Style\SymfonyStyle;
-
-#[AsCommand(
-    name: 'blog:slug:init',
-    description: 'Add a short description for your command',
-)]
-class BlogSlugInitCommand extends Command
-{
-    public function __construct(
-        protected readonly SluggerService $sluggerService,
-        protected readonly ArticleRepository $articleRepository
-    )
-    {
-        parent::__construct();
-    }
-
-    protected function execute(InputInterface $input, OutputInterface $output): int
-    {
-        $io = new SymfonyStyle($input, $output);
-        
-        $this->sluggerService->generateSlugsForExistingArticles();
-
-        $articles = $this->articleRepository->findAll();
-        foreach($articles as $article) {
-            $io->text(sprintf('Article : %s / Slug : %s', $article->getTitle(), $article->getSlug()));
-        }
-
-        $io->success('Les slugs sont générés');
-
-        return Command::SUCCESS;
-    }
-}

+ 2 - 1
src/Entity/Article.php

@@ -7,6 +7,7 @@ use Doctrine\DBAL\Types\Types;
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
 
+#[UniqueEntity('slug')]
 #[ORM\Entity(repositoryClass: ArticleRepository::class)]
 class Article
 {
@@ -31,7 +32,7 @@ class Article
     #[ORM\Column(length: 64)]
     private ?string $state = null;
 
-    #[ORM\Column(length: 100, unique: false)]
+    #[ORM\Column(length: 100, unique: true)]
     private ?string $slug = null;
 
     public function __construct(User $author)

+ 0 - 40
src/Service/SluggerService.php

@@ -1,40 +0,0 @@
-<?php
-
-// src/Service/SluggerService.php
-
-namespace App\Service;
-
-use App\Entity\Article;
-use Doctrine\ORM\EntityManagerInterface;
-use Symfony\Component\String\Slugger\SluggerInterface;
-
-class SluggerService
-{
-    public function __construct(
-        protected readonly EntityManagerInterface $entityManager,
-        protected readonly SluggerInterface $slugger)
-    {
-    }
-
-    public function generateSlugsForExistingArticles()
-    {
-        $articles = $this->entityManager->getRepository(Article::class)->findAll();
-
-        foreach ($articles as $article) {
-            $slug = $this->slugger->slug($article->getTitle())->lower();
-            $existingSlug = $slug;
-
-            // Vérification des conflits
-            $count = 1;
-            while ($this->entityManager->getRepository(Article::class)->findOneBy(['slug' => $existingSlug])) {
-                $existingSlug = $slug . '-' . $count;
-                $count++;
-            }
-
-            $article->setSlug($existingSlug);
-            $this->entityManager->persist($article);
-        }
-
-        $this->entityManager->flush();
-    }
-}