下面的代码取自 Symfony 6 文档https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database
// src/Controller/ProductController.php
namespace App\Controller;
// ...
use App\Entity\Product;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Response;
class ProductController extends AbstractController
{
/**
* @Route("/product", name="create_product")
*/
public function createProduct(ManagerRegistry $doctrine): Response
{
$entityManager = $doctrine->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());
}
}
我的问题如下:
开发人员是如何来
ManagerRegistry
为这个项目挑选课程的,因为通过课程,我找不到任何有用的标准或出色的东西。请注意上面的代码运行良好,我只想知道伴随类选择的思维模式ManagerRegistry
。接下来,开发者为什么要这样做
$entityManager = $doctrine->getManager();
,他从中得到了什么。最后,我得到
dd()
了一些奇怪的输出。它们是什么,我该如何使用它们。$doctrine
$entityManager