当前位置:网站首页>How do I delete an entity from symfony2

How do I delete an entity from symfony2

2022-06-25 19:57:00 Superior virtue and weak water

problem :

My first symfony2 project is a list of guests (invited in an event) stored in a database. My first one symfony2 Projects are stored in a database guest List of virtual machines ( Invited in the event ).I have I have a

  • created the entity class Guest with all variables for them (id, name, address, phone number etc.) Created entity class Guest, Include all variables (id,name,address,phone number etc. )
  • created the schema in the mysql db stay mysql db The schema is created in
  • created a route for "adding a guest" to a twig template Created... For the branch template “ Add visitors ” The route of the
  • created a formType Created a formType

and finally a "createGuest" method in the Controller and everything works fine. Last in Controller Use in “createGuest” Method , Everything is all right .

I can't manage to remove a guest from the database. I can't delete... From the database guest virtual machine .I have read every tutorial in the web, including the official Symfony2 book; I have read every tutorial on the Internet , Including the official Symfony2 book ;all that it says is : All it says is :

Deleting an Object Delete object

Deleting an object is very similar, but requires a call to the remove() method of the entity manager: Deleting objects is very similar , But you need to call the entity manager remove() Method :

$em->remove($product);$em->flush();

It does not say anything more than that (even the "Update an object" section is missing documentation) on how to connect the controller deleteAction($id) with the twig template. About how to put the controller deleteAction($ id) And twig The templates are connected , It doesn't say ( even to the extent that “ Update the object ” Some documents are missing ).What I want to do is to list all guests with a viewGuests action and a viewGuests twig template, having a delete icon next to every row, which you should click to delete an entry. What I want to do is to list all the items that have viewGuests Operation and viewGuests The guest of the branch template , There is a delete icon next to each line , You should click the icon to delete the entry .Simple, but I cannot find any documentation and do not know where to start from. It's simple , But I can't find any documents , I don't know where to start .

public function deleteGuestAction($id)    {        $em = $this->getDoctrine()->getEntityManager();        $guest = $em->getRepository('GuestBundle:Guest')->find($id);        if (!$guest) {            throw $this->createNotFoundException('No guest found for id '.$id);        }        $em->remove($guest);        $em->flush();        return $this->redirect($this->generateUrl('GuestBundle:Page:viewGuests.html.twig'));    }

Solution :

Reference resources : https://stackoom.com/en/question/nYEn
原网站

版权声明
本文为[Superior virtue and weak water]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190511378744.html