Bonjour,

Je dois créer un site capable de gérer des plannings de projet. Je possède donc 3 entités : Collaborateur, Projet et Tache. Collaborateur est lié en ManyToMany avec Projet et Tache. Projet et Tache sont lié en OneToMany (Projet = One, Tache = Many).

Collaborateur :

<?php

namespace App\Entity;

use App\Repository\CollaborateurRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=CollaborateurRepository::class)
 */
class Collaborateur
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

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

    public function getNom(): ?string
    {
        return $this->nom;
    }

    public function setNom(string $nom): self
    {
        $this->nom = $nom;

        return $this;
    }

    public function getPrenom(): ?string
    {
        return $this->prenom;
    }

    public function setPrenom(string $prenom): self
    {
        $this->prenom = $prenom;

        return $this;
    }

    public function __toString()
    {
        return $this->nom;
    }
}

Projet :

<?php

namespace App\Entity;

use App\Repository\ProjetRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=ProjetRepository::class)
 */
class Projet
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\Column(type="text")
     */
    private $description;

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

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

    /**
     * @ORM\ManyToMany(targetEntity=Collaborateur::class)
     */
    private $collab;

    /**
     * @ORM\OneToMany(targetEntity=Tache::class, mappedBy="projet")
     */
    private $taches;

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

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

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getColor(): ?string
    {
        return $this->color;
    }

    public function setColor(string $color): self
    {
        $this->color = $color;

        return $this;
    }

    public function getTextColor(): ?string
    {
        return $this->text_color;
    }

    public function setTextColor(string $text_color): self
    {
        $this->text_color = $text_color;

        return $this;
    }

    /**
     * @return Collection|Collaborateur[]
     */
    public function getCollab(): Collection
    {
        return $this->collab;
    }

    public function addCollab(Collaborateur $collab): self
    {
        if (!$this->collab->contains($collab)) {
            $this->collab[] = $collab;
        }

        return $this;
    }

    public function removeCollab(Collaborateur $collab): self
    {
        $this->collab->removeElement($collab);

        return $this;
    }

    /**
     * @return Collection|Tache[]
     */
    public function getTaches(): Collection
    {
        return $this->taches;
    }

    public function addTach(Tache $tach): self
    {
        if (!$this->taches->contains($tach)) {
            $this->taches[] = $tach;
            $tach->setProjet($this);
        }

        return $this;
    }

    public function removeTach(Tache $tach): self
    {
        if ($this->taches->removeElement($tach)) {
            // set the owning side to null (unless already changed)
            if ($tach->getProjet() === $this) {
                $tach->setProjet(null);
            }
        }

        return $this;
    }

    public function __toString()
    {
        return $this->title;
    }
}

Je récupère un Projet et ensuite j'aimerais récupérer les Collaborateurs liés, notamment leur Id et Nom. Seulement je ne sais absolument pas comment faire. Si quelqu'un peut m'aider je lui en serai très reconnaissant.

Merci d'avance.

2 réponses


freda_73
Réponse acceptée

Une fois que tu as ton projet tu utilises la fonction getCollab de ton Entity Projet

$lesCollab = $leProjet->getCollab();
// puis :
foreach($lesCollab as $collab) {
dump($collab->getNom());
// le dump c'est pour l'exemple ;-)
}

Zombizz
Auteur

Merci de ta réponse j'ai réussi à obtenir ce que je voulais