Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // src/Controller/ProductController.php
- namespace App\Controller;
- // ...
- use App\Entity\Product;
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Component\HttpFoundation\Response;
- class ProductController extends AbstractController
- {
- /**
- * @Route("/product", name="create_product")
- */
- public function createProduct(): Response
- {
- // you can fetch the EntityManager via $this->getDoctrine()
- // or you can add an argument to the action: createProduct(EntityManagerInterface $entityManager)
- $entityManager = $this->getDoctrine()->getManager();
- $product = new Product();
- $product->setName('Keyboard');
- $product->setPrice(1999);
- $product->setDescription('Ergonomic and stylish!');
- // tell Doctrine you want to (eventually) save the Product (no queries yet)
- $entityManager->persist($product);
- // actually executes the queries (i.e. the INSERT query)
- $entityManager->flush();
- return new Response('Saved new product with id '.$product->getId());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement