Bonjour,

J'ai une erreur Symfony qui me bloque dans l'avancée de mon projet. Je souhaite que quand l'utilisateur clique sur un bouton pour valider une ligne située dans un tableau, que le texte "validé" s'écrive dans la colonne "état" de la base de données. Cependant, j'obtiens l'erreur suivante quand je clique sur ce bouton : Attempted to call an undefined method named "setEtat" of class "ContainerT1w6wEj\EntityManager_9a5be93".

Mon controller est le suivant :

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\SuiviFrais;
use App\Entity\ElementsForfaitises;
use App\Entity\ElementsHorsForfait;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;

class SuiviFraisController extends AbstractController
{
    private $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    /**
     * @Route("/suivifrais/etat/valide{valide1}", name = "app_suivi_frais_etat_valide")
    */
    public function frais_hors_forfait_valide($valide1):Response
    {
        $valide2 = $this->getDoctrine()->getRepository(ElementsHorsForfait::class)->findOneById($valide1);

        $this->em->setEtat('Validé');
        $this->em->flush();
        return $this->redirectToRoute('app_suivi_frais');
    }
}

Mon twig est le suivant :

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

{% block title %}Galaxy Swiss Bourdin | Suivi de mes frais{% endblock %}

{% block body %}

<head>
    <meta charset='utf-8'>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">    <link rel="stylesheet" href="/css/style.css">
</head>

<body>
    <div class="container-fluid">
        <div class="row">
           {% include 'include/sidebar.html.twig' %}  
            <div class="col-9">
                <br><br><br>
                <h2>Suivi frais</h2>
                <br><br><br><h4>Éléments forfaitisés</h4>
                    <div class="table-responsive-sm">
                    <table class="table table-hover">
                        <br><br><br><thead>
                            <tr>
                                <th>#</th>
                                <th>Forfait étape</th>
                                <th>Frais kilométrique</th>
                                <th>Nuitée hôtel</th>
                                <th>Repas restaurant</th>
                                {% if is_granted('ROLE_VISITEUR') %}
                                <th>Action</th>
                                {% endif %}
                                {% if is_granted('ROLE_COMPTABLE') %}
                                <th>Actions</th>
                                {% endif %}
                            </tr>
                        </thead>
                        <tbdody>
                        {% for suivi_frais in ElementsForfaitises %}
                            <tr>
                                <td>{{ suivi_frais.id }}</td>
                                <td>{{ suivi_frais.forfaitetape }}</td>
                                <td>{{ suivi_frais.fraiskilometrique }}</td>
                                <td>{{ suivi_frais.nuiteehotel }}</td>
                                <td>{{ suivi_frais.repasrestaurant }}</td>
                                {% if is_granted('ROLE_VISITEUR') %}
                                <td><a href="{{ path('app_suivi_frais_forfaitises_delete_ef', {'ef':suivi_frais.id}) }}"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">
                                <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/>
                                <path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/>
                                </svg></a></td>
                                {% endif %}
                            </tr>
                        {% endfor %}
                        </tbdody>
                    </table>
                    </div>
                    <br><br><br><h4>Éléments hors forfait</h4>
                    <div class="table-responsive-sm">
                    <table class="table table-hover">
                        <br><br><br><thead>
                            <tr>
                                <th>#</th>
                                <th>Date</th>
                                <th>Libellé</th>
                                <th>Montant</th>
                                {% if is_granted('ROLE_VISITEUR') %}
                                <th>Action</th>
                                {% endif %}
                                {% if is_granted('ROLE_COMPTABLE') %}
                                <th>Actions</th>
                                {% endif %}
                            </tr>
                        </thead>
                        <tbdody>
                        {% for suivi_frais in ElementsHorsForfait %}
                            <tr>
                                <td>{{ suivi_frais.id }}</td>
                                <td>{{ suivi_frais.date|date('m/Y') }}</td>
                                <td>{{ suivi_frais.libelle }}</td>
                                <td>{{ suivi_frais.montant }}</td>
                                {% if is_granted('ROLE_VISITEUR') %}
                                <td><a href="{{ path('app_suivi_frais_horsforfait_delete_ehf', {'ehf':suivi_frais.id}) }}"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">
                                <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/>
                                <path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/>
                                </svg></a></td>
                                {% endif %}
                                {% if is_granted('ROLE_COMPTABLE') %}
                                <td><a href="{{ path('app_suivi_frais_etat_valide', {'valide1':suivi_frais.id}) }}"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">
                                <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/>
                                <path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/>
                                </svg></a></td>
                                {% endif %}
                            </tr>
                        {% endfor %}
                        </tbdody>
                    </table>
                    </div>
            </div>
        </div>
    </div>
    </div>
</body>
</style>
{% endblock %}

Merci par avance pour votre aide !

2 réponses


gillesr
Réponse acceptée

Bonjour,

il ne faut pas appeler la méthode setEtat() sur l'EntityManagerInterface mais sur ton entité à savoir $valide2, donc remplace :


$this->em->setEtat('Validé');

par


$valide2>setEtat('Validé');
alexwtrs
Auteur

Bonjour gillesr,

Merci beaucoup. Ça fonctionne !