How to Display Comments with Foreign Key "jeu_id" using Symfony 5

How to Display Comments with Foreign Key “jeu_id” using Symfony 5

In this article, we will explore how to display comments associated with a specific game in Symfony 5. We will also cover the use of foreign keys and entities to establish relationships between tables.

Introduction

Symfony is a popular PHP framework for building web applications. In this example, we will create a blog that displays information about games, including comments left by users. To achieve this, we need to understand how to work with entities, relationships, and routing in Symfony 5.

Setting Up the Database

First, let’s set up our database. We will assume you have a jeux (game) entity with an id primary key, a commentaires (comments) entity with a foreign key jeu_id, and another entity called CommentaireType that represents individual comments.

// Jeux.php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\DoctrineBundle\SupportEntityRepositoryInterface;

/**
 * @ORM\Entity(repositoryClass="App\Repository\JeuxRepository")
 */
class Jeux implements EntityRepositoryInterface
{
    // ...
}
// Commentaires.php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Common\Model\AbstractModel;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CommentairesRepository")
 */
class Commentaires extends AbstractModel
{
    // ...
}
// CommentaireType.php

namespace App\Form;

use App\Entity\CommentaireType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CommentaireType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // ...
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        // ...
    }
}

Creating the Route

Next, let’s create a route to display comments for a specific game. We will use the @Route annotation to define the route and pass the game ID as a parameter.

// FicheController.php

namespace App\Controller;

use App\Entity\Jeux;
use App\Form\CommentaireType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class FicheController extends AbstractController
{
    /**
     * @Route("/fiche/{jeux}", name="fiche", methods={"POST", "GET"})
     */
    public function fiche(Jeux $jeux, Request $request): Response
    {
        // ...
    }
}

Displaying Comments

Now that we have our route set up, let’s display comments for a specific game. We will use the getDoctrine method to retrieve the commentaires entity and fetch comments associated with the current jeu.

// FicheController.php (continued)

public function fiche(Jeux $jeux, Request $request): Response
{
    // ...

    if ($form->isSubmitted() && $form->isValid()) {
        // ...
    }

    return $this->render('main/fiche.html.twig', [
        'commentaires' => $jeux->getCommentaires(),
        'jeux' => $jeux,
        'form' => $form->createView(),
    ]);
}
// main/fiche.html.twig

{% for commentaire in object.commentaires %}
  <!-- Display the comment -->
  {{ commentaire.texte }}
{% endfor %}

Conclusion

In this article, we have covered how to display comments associated with a specific game using Symfony 5. We discussed the use of foreign keys and entities to establish relationships between tables, as well as how to create routes and display data in Twig templates.

By following these steps and examples, you should be able to build your own comment system for games in Symfony 5.


Last modified on 2023-09-20