CoderTn
CoderTn

Reputation: 997

delete row from a database table in symfony

i want to delete the selected row from an html table that displays the orders list . this is the table code :

 {% for reservation in ListeDesReservations %}
            <tr>
    //columns ... //
                <td class="text-center text-lg text-medium">{{ reservation.seat}}</td>
                <td class="text-center text-lg text-medium">{{ reservation.getEvent().getPrix()}}</td>
              {% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
                {% set url = absolute_url(asset('')) ~ 'app_dev.php/supprimerReservation/' ~ reservation.id  %}
              {% else %}
                {% set url = "OTHER_URL" %}
              {% endif %}
                <td class="text-center"><a  href="{{url}}" ></a></td>
              </tr>
            {% endfor %}
            </tbody>
          </table>

this my delete method in the controller :

    public function SupprimerReservationAction($idReservation){
        $em = $this->getDoctrine()->getEntityManager();
        $entite = $em->getRepository('techeventBundle:reservation')->find($idReservation);
        $em->remove($entite);
        $em->persist($entite);
        $em->flush();
        //Affichage
        $iduser = $this->getUser()->getId();
        return $this->redirectToRoute('affichage', ['iduser' => $iduser]);
        //Affichage
    }

this is my routing file :

affichage:
    path:     /afficherPanier/{iduser}
    defaults: { _controller: reservationBundle:Default:afficherPanier }

supprimerReservation:
    path:     /supprimerReservation/{idReservation}
    defaults: { _controller: reservationBundle:Default:SupprimerReservation }

the problem is when i click on the remove link it redirects me to the same link (app_dev.php/afficherPanier/2) while it's supposed to redirect me to the delete route (app_dev.php/supprimerReservation/27)

Upvotes: 0

Views: 3644

Answers (2)

Preciel
Preciel

Reputation: 2837

I would suggest to work with annotations for routes. Makes things easier.

What you're doing is far from being the proper way to go about it.

Your delete fonction will not work properly either...
You're deleting with $em->remove($entite), then creating with $em->persist($entite).
Doing this won't ends well for sure.

Your twig is a mess...
We don't create URLs the way you did. There is the twig extension path to do it.

I would suggest to start reading about Symfony :

Creating a Simple Form

Persist/Fetch/Update/Delete an Object in the Database

Controllers

The links above are for Symfony 3.4.
Make sure to change the version with the dropdown select on the page to your corresponding Symfony version if you're not using Symfony 3.4

Upvotes: 0

Cid
Cid

Reputation: 15257

In your controller action you are doing :

$em->remove($entite);
$em->persist($entite);

You are removing then saving the entity. Remove the $em->persist($entite); line.


Your redirection is a normal behaviour since you are requesting it in your controller action return $this->redirectToRoute('affichage', ['iduser' => $iduser]);


Note : This is not how is supposed to be build a path {% set url = absolute_url(asset('')) ~ 'app_dev.php/supprimerReservation/' ~ reservation.id %}

Twig has a method named path that allows you to build URLs based on the route name

{% set url = path('supprimerReservation', {'idReservation': reservation.id }) %}

Upvotes: 1

Related Questions