title: How do I delete an entity from symfony2
body: <p>My first symfony2 project is a list of guests (invited in an event) stored in a database. I have </p>
  
  <ul>
  <li>created the entity class Guest with all variables for them (id, name, address, phone number etc.) </li>
  <li>created the schema in the mysql db</li>
  <li>created a route for "adding a guest" to a twig template</li>
  <li>created a formType </li>
  </ul>
  
  <p>and finally a "createGuest" method in the Controller and everything works fine. </p>
  
  <p>I can't manage to remove a guest from the database. I have read every tutorial in the web, including the official Symfony2 book; all that it says is :</p>
  
  <p><strong>Deleting an Object</strong></p>
  
  <p><em>Deleting an object is very similar, but requires a call to the remove() method of the entity manager:</em></p>
  
  <pre class="lang-php prettyprint-override"><code>$em-&gt;remove($product);
  $em-&gt;flush();
  </code></pre>
  
  <p>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. 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. Simple, but I cannot find any documentation and do not know where to start from. </p>
  
  <pre class="lang-php prettyprint-override"><code>public function deleteGuestAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
          $guest = $em-&gt;getRepository('GuestBundle:Guest')-&gt;find($id);
  
          if (!$guest) {
              throw $this-&gt;createNotFoundException('No guest found for id '.$id);
          }
  
          $em-&gt;remove($guest);
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('GuestBundle:Page:viewGuests.html.twig'));
      }
  </code></pre>