Bonjour,

J'ai un soucis au niveau de la récupération de la clé étrangère au sein d'une table.

Exemple :
J'ai une table catégorie et et une table article
Une catérgorie peut avoir plusieurs articles mais un article ne peut avoir qu'une seule catégorie.

Dans ma table article j'ai une clé categorie_id qui est lié à la clé id de ma table categorie par une relation OneToMany

Maintenant quand je vais dans twig j'arrive à afficher toutes les donnée en rapport avec un article, sauf la valeur du categorie_id.

categorie_id étant généré par une relation, si j'ai bien il donne forcément un objet.

SAUF que je n'arrive pa à trouver le filtre qui me permet d'afficher la valeur de la clé categorie_id ou encore mieux, la valeur du champs categorie.titre correspondant

Comment puis-je affcher de categorie_id la valeur du champs categorie.titre correspondant lui correspondant ?

                {% for Chapitre in ChapitreCours %}
                <tr>
                    <td>
                        {{Chapitre.titre}}
                    </td>
                    <td>
                        {{Chapitre.problematique}}
                    </td>
                        {{Chapitre.Partie}}
                    <td>
                    </td>
                    <td>
                        {{Chapitre.position}}
                    </td>
                    <td>
                        <a class= "btn btn-primary" href="{{path('chapitre_edit', {id: Chapitre.id })}}">Editer</a>
                    </td>

                <tr>

                </tr>
                {% endfor %}

Le problème se pose à {{Chapitre.Partie}}
$Partie est la propriété qui (dans mon entity Article) est lié à categorie_id dans ma table article.

7 réponses


Digivia
Réponse acceptée

Essaye {{Chapitre.Partie.partie}}
Le champ de ton association est Chapitre.Partie, mais twig attend un champ de cette entité.
Ou il faut définir une méthode __toString() dans ton entité ShikomoriCoursParties

Devdeb22
Auteur

Voici l'érreur que j'ai :

"Catchable Fatal Error: Object of class Proxies__CG__\App\Entity\ShikomoriCoursParties could not be converted to string"

Même en utilisant la fonction __toString J'ai toujours le même problème.

Edit : "Une catérgorie peut avoir plusieurs articles mais un article ne peut avoir qu'une seule catégorie."
C'est donc une relation ManyToOne entre Article et Catégorie...

Devdeb22
Auteur

Je viens de faire la modification ça me donne toujours l'erreur could not be converted to string

Devdeb22
Auteur

Voici mon code twig

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

{% block title %}Gerer les chapitres{% endblock %}

{% block body %}

    <div class="container-fluid mt-4">

        <h1>Gerer les chapitres</h1>

        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Partie</th>
                    <th>Position</th>
                    <th>Nom</th>
                    <th>Problématique</th>
                    <th>Action</th>
                </tr>

            </thead>
            <tbody>
                {% for Chapitre in ChapitreCours %}
                <tr>
                    <td>
                        {{Chapitre.Partie}}
                    </td>
                    <td>
                        {{Chapitre.position}}
                    </td>
                    <td>
                        {{Chapitre.titre}}
                    </td>
                    <td>
                        {{Chapitre.problematique}}  
                    </td>
                    <td>
                        <a class= "btn btn-primary" href="#">Editer</a>
                    </td>   
                <tr>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
{% endblock %}

Et voici la classe de mon Entity

<?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\ShikomoriCoursChapitreRepository")
 */
class ShikomoriCoursChapitre
{

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\ShikomoriCoursParties", inversedBy="Chapitre")
     * @ORM\JoinColumn(nullable=false)
     */
    private $Partie;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\ShikomoriCoursSousChapitre", mappedBy="Chapitre")
     */
    private $SousChapitre;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\ShikomoriCoursLecon", mappedBy="Chapitre")
     */
    private $Lecon;

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

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

    /**
     * @ORM\Column(type="integer")
     */
    private $position;

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

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

    public function getPartie(): ?ShikomoriCoursParties
    {
        return $this->Partie;
    }

    public function setPartie(?ShikomoriCoursParties $Partie): self
    {
        $this->Partie = $Partie;

        return $this;
    }

    /**
     * @return Collection|ShikomoriCoursSousChapitre[]
     */
    public function getSousChapitre(): Collection
    {
        return $this->SousChapitre;
    }

    public function addSousChapitre(ShikomoriCoursSousChapitre $sousChapitre): self
    {
        if (!$this->SousChapitre->contains($sousChapitre)) {
            $this->SousChapitre[] = $sousChapitre;
            $sousChapitre->setChapitre($this);
        }

        return $this;
    }

    public function removeSousChapitre(ShikomoriCoursSousChapitre $sousChapitre): self
    {
        if ($this->SousChapitre->contains($sousChapitre)) {
            $this->SousChapitre->removeElement($sousChapitre);
            // set the owning side to null (unless already changed)
            if ($sousChapitre->getChapitre() === $this) {
                $sousChapitre->setChapitre(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|ShikomoriCoursLecon[]
     */
    public function getLecon(): Collection
    {
        return $this->Lecon;
    }

    public function addLecon(ShikomoriCoursLecon $lecon): self
    {
        if (!$this->Lecon->contains($lecon)) {
            $this->Lecon[] = $lecon;
            $lecon->setChapitre($this);
        }

        return $this;
    }

    public function removeLecon(ShikomoriCoursLecon $lecon): self
    {
        if ($this->Lecon->contains($lecon)) {
            $this->Lecon->removeElement($lecon);
            // set the owning side to null (unless already changed)
            if ($lecon->getChapitre() === $this) {
                $lecon->setChapitre(null);
            }
        }

        return $this;
    }

    public function getTitre(): ?string
    {
        return $this->titre;
    }

    public function setTitre(string $titre): self
    {
        $this->titre = $titre;

        return $this;
    }

    public function getProblematique(): ?string
    {
        return $this->problematique;
    }

    public function setProblematique(string $problematique): self
    {
        $this->problematique = $problematique;

        return $this;
    }

    public function getPosition(): ?int
    {
        return $this->position;
    }

    public function setPosition(int $position): self
    {
        $this->position = $position;

        return $this;
    }

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

Je ne sais pas si queqlun peut m'aider.

Devdeb22
Auteur

Lol merci.

Je viens tout juste de trouver la solution.

Merci quand-même pour ton aide ☺

Devdeb22
Auteur

C'est chiant quand tu passes trois jours à chercher un truc qui en faite est tout facile