Ceci est une ancienne révision du document !
Opérations CRUD
Entity manager & Repository
Entity manager
Récupération de l'entity manager doctrine dans un contrôleur :
// you can fetch the EntityManager via $this->getDoctrine()
// or you can add an argument to your action: index(EntityManagerInterface $em)
$em = $this->getDoctrine()->getManager();
Comme indiqué, l'entity manager peut aussi être injecté.
Repository
// ...
/**
* @Route("/product/{id}", name="product_show")
*/
public function showAction($id)
{
$product = $this->getDoctrine()
->getRepository(Product::class)
->find($id);
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
return new Response('Check out this great product: '.$product->getName());
// or render a template
// in the template, print things with {{ product.name }}
// return $this->render('product/show.html.twig', ['product' => $product]);
}