Bonjour tout le monde,

Je début avec Symfony 4 et je bug sur un problème depuis le début de la journée ..
Alors voilà j'ai deux entitées Classe et Stagiaire qui ont une relation ManyToMany du coup symfony m'as créer une 3eme table dans ma base de données qui se nomme classe_stagiaire.

J'ai fais un controller qui me permet de d'afficher tout mes stagiaires mais en plus de ça j'aimerai aussi afficher la classe du stagiaire sauf que je n'ai rien et je comprend pas où est ce que j'ai pus faire une erreur.

Merci d'avance cordialement
iggor


<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\StagiaireRepository")
 */
class Stagiaire
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $firstName;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $lastName;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $age;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $sexe;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $phone;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $skill;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Classe", mappedBy="stagiares")
     */
    private $classes;

    public function __construct()
    {
        $this->classes = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getFirstName(): ?string
    {
        return $this->firstName;
    }

    public function setFirstName(string $firstName): self
    {
        $this->firstName = $firstName;

        return $this;
    }

    public function getLastName(): ?string
    {
        return $this->lastName;
    }

    public function setLastName(string $lastName): self
    {
        $this->lastName = $lastName;

        return $this;
    }

    public function getAge(): ?string
    {
        return $this->age;
    }

    public function setAge(string $age): self
    {
        $this->age = $age;

        return $this;
    }

    public function getSexe(): ?string
    {
        return $this->sexe;
    }

    public function setSexe(string $sexe): self
    {
        $this->sexe = $sexe;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(string $phone): self
    {
        $this->phone = $phone;

        return $this;
    }

    public function getSkill(): ?string
    {
        return $this->skill;
    }

    public function setSkill(string $skill): self
    {
        $this->skill = $skill;

        return $this;
    }



    // public function __toString()
    // {
    //     return $this->getClasse();
    // }
    
    /**
     * @return Collection|Classe[]
     */
    public function getClasses(): Collection
    {
        return $this->classes;
    }

    public function setClasses(?Classe $classe): self
    {
        $this->classe = $classe;
        return $this;
    }

    public function addClass(Classe $class): self
    {
        if (!$this->classes->contains($class)) {
            $this->classes[] = $class;
            $class->addStagiare($this);
        }

        return $this;
    }

    public function removeClass(Classe $class): self
    {
        if ($this->classes->contains($class)) {
            $this->classes->removeElement($class);
            $class->removeStagiare($this);
        }

        return $this;
    }
}




<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ClasseRepository")
 * @UniqueEntity(
 * fields={"name"},
 * message="Ce nom de classe existe déjà")
 *  @UniqueEntity(
 * fields={"dateStart"},
 * message="Cette classe existe déjà")
 */
class Classe
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\Column(type="datetime")
     */
    private $dateStart;

    /**
     * @ORM\Column(type="datetime")
     */
    private $dateEnd;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $nbPlace;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Stagiaire", inversedBy="classes")
     */
    private $stagiares;

    public function __construct()
    {
        $this->stagiares = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getDateStart(): ?\DateTimeInterface
    {
        return $this->dateStart;
    }

    public function setDateStart(\DateTimeInterface $dateStart): self
    {
        $this->dateStart = $dateStart;

        return $this;
    }

    public function getDateEnd(): ?\DateTimeInterface
    {
        return $this->dateEnd;
    }

    public function setDateEnd(\DateTimeInterface $dateEnd): self
    {
        $this->dateEnd = $dateEnd;

        return $this;
    }

    public function getNbPlace(): ?string
    {
        return $this->nbPlace;
    }

    public function setNbPlace(string $nbPlace): self
    {
        $this->nbPlace = $nbPlace;

        return $this;
    }

    /**
     * @return Collection|Stagiaire[]
     */
    public function getStagiares(): Collection
    {
        return $this->stagiares;
    }

    public function addStagiare(Stagiaire $stagiare): self
    {
        if (!$this->stagiares->contains($stagiare)) {
            $this->stagiares[] = $stagiare;
        }

        return $this;
    }

    public function removeStagiare(Stagiaire $stagiare): self
    {
        if ($this->stagiares->contains($stagiare)) {
            $this->stagiares->removeElement($stagiare);
        }

        return $this;
    }

   
}


 /**
     * @Route("/admin/listStagiaire", name="all_stagiaire")
     */
    public function showAllStagiaire(StagiaireRepository $repo)
    {

        $stagiaires = $repo->findAll();
        return $this->render('/stagiaire/showAll.html.twig',[

            'stagiaires' => $stagiaires
        



{% extends "base.html.twig" %}

{% block title %}Liste des stagiaires{% endblock %}

{% block body %}
<div id="listStagiaiare" class="container-fluid">
    <div class="container">
        <div class="row">
            <div class="group_title offset-3 col-6">
                <h1>Liste des stagiaiares</h1>
            </div>


            {% for stagiaire in stagiaires %}
            <div class="col-4">
                <h2>{{ stagiaire.firstName }}</h2>

                <ul>
                    <li>{{ stagiaire.lastName }}</li>
                    <li>{{ stagiaire.age }}</li>
                    {% for class in stagiaire.classes %}
                    <p>classe: {{ class.name }}</p>

                    {% endfor %}
                    <li>{{ stagiaire.phone }}</li>
                </ul>
            </div>
            {% endfor %}



        </div>
    </div>
</div>
{% endblock %}

Modifié par iggor (25 Sep 2019 - 14:35)