Skip to content

Commit

Permalink
Add tag slug
Browse files Browse the repository at this point in the history
  • Loading branch information
AchillesKal committed Apr 7, 2024
1 parent ef8a3b4 commit 608872d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
28 changes: 28 additions & 0 deletions migrations/Version20240407143332.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20240407143332 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE tag ADD slug VARCHAR(255) NOT NULL');
$this->addSql('CREATE UNIQUE INDEX UNIQ_389B783989D9B62 ON tag (slug)');
}

public function down(Schema $schema): void
{
$this->addSql('DROP INDEX UNIQ_389B783989D9B62');
$this->addSql('ALTER TABLE tag DROP slug');
}
}
2 changes: 1 addition & 1 deletion src/Controller/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function new(Request $request, EntityManagerInterface $entityManager): Re
]);
}

#[Route('/{id}', name: 'app_tag_show', methods: ['GET'])]
#[Route('/{slug}', name: 'app_tag_show', methods: ['GET'])]
public function show(Tag $tag): Response
{
return $this->render('tag/show.html.twig', [
Expand Down
17 changes: 17 additions & 0 deletions src/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation\Slug;

#[ORM\Entity(repositoryClass: TagRepository::class)]
class Tag
Expand All @@ -21,6 +22,10 @@ class Tag
#[ORM\Column(length: 255)]
private string $title;

#[ORM\Column(length: 255, unique: true)]
#[Slug(fields: ['title'])]
private string $slug;

#[ORM\ManyToMany(targetEntity: BlogPost::class, mappedBy: 'tags')]
private Collection $blogPosts;

Expand All @@ -47,6 +52,18 @@ public function setTitle(string $title): static
return $this;
}

public function getSlug(): string
{
return $this->slug;
}

public function setSlug(string $slug): static
{
$this->slug = $slug;

return $this;
}

/**
* @return Collection<int, BlogPost>
*/
Expand Down
2 changes: 1 addition & 1 deletion templates/tag/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<td>{{ tag.createdAt ? tag.createdAt|date('Y-m-d H:i:s') : '' }}</td>
<td>{{ tag.updatedAt ? tag.updatedAt|date('Y-m-d H:i:s') : '' }}</td>
<td>
<a href="{{ path('app_tag_show', {'id': tag.id}) }}">show</a>
<a href="{{ path('app_tag_show', {'slug': tag.slug}) }}">show</a>
<a href="{{ path('app_tag_edit', {'id': tag.id}) }}">edit</a>
</td>
</tr>
Expand Down

0 comments on commit 608872d

Please sign in to comment.