How do I delete an entity from symfony2

Solution 1:

Symfony is smart and knows how to make the find() by itself :

public function deleteGuestAction(Guest $guest)
{
    if (!$guest) {
        throw $this->createNotFoundException('No guest found');
    }

    $em = $this->getDoctrine()->getEntityManager();
    $em->remove($guest);
    $em->flush();

    return $this->redirect($this->generateUrl('GuestBundle:Page:viewGuests.html.twig'));
}

To send the id in your controller, use {{ path('your_route', {'id': guest.id}) }}

Solution 2:

DELETE FROM ... WHERE id=...;

protected function templateRemove($id){
    $em = $this->getDoctrine()->getManager();
    $entity = $em->getRepository('XXXBundle:Templates')->findOneBy(array('id' => $id));

    if ($entity != null){
        $em->remove($entity);
        $em->flush();
    }
}