Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. // src/Controller/ProductController.php
  2. namespace App\Controller;
  3.  
  4. // ...
  5. use App\Entity\Product;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Response;
  8.  
  9. class ProductController extends AbstractController
  10. {
  11.     /**
  12.      * @Route("/product", name="create_product")
  13.      */
  14.     public function createProduct(): Response
  15.     {
  16.         // you can fetch the EntityManager via $this->getDoctrine()
  17.         // or you can add an argument to the action: createProduct(EntityManagerInterface $entityManager)
  18.         $entityManager = $this->getDoctrine()->getManager();
  19.  
  20.         $product = new Product();
  21.         $product->setName('Keyboard');
  22.         $product->setPrice(1999);
  23.         $product->setDescription('Ergonomic and stylish!');
  24.  
  25.         // tell Doctrine you want to (eventually) save the Product (no queries yet)
  26.         $entityManager->persist($product);
  27.  
  28.         // actually executes the queries (i.e. the INSERT query)
  29.         $entityManager->flush();
  30.  
  31.         return new Response('Saved new product with id '.$product->getId());
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement