Skip to content

[Doctrine] replace ManagerRegistry in doctrine associations doc #18091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions doctrine/associations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,14 @@ Now you can see this new code in action! Imagine you're inside a controller::
// ...
use App\Entity\Category;
use App\Entity\Product;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ProductController extends AbstractController
{
#[Route('/product', name: 'product')]
public function index(ManagerRegistry $doctrine): Response
public function index(EntityManagerInterface $entityManager): Response
{
$category = new Category();
$category->setName('Computer Peripherals');
Expand All @@ -333,7 +333,6 @@ Now you can see this new code in action! Imagine you're inside a controller::
// relates this product to the category
$product->setCategory($category);

$entityManager = $doctrine->getManager();
$entityManager->persist($category);
$entityManager->persist($product);
$entityManager->flush();
Expand Down Expand Up @@ -379,9 +378,9 @@ before. First, fetch a ``$product`` object and then access its related

class ProductController extends AbstractController
{
public function show(ManagerRegistry $doctrine, int $id): Response
public function show(ProductRepository $productRepository, int $id): Response
{
$product = $doctrine->getRepository(Product::class)->find($id);
$product = $productRepository->find($id);
// ...

$categoryName = $product->getCategory()->getName();
Expand Down Expand Up @@ -412,9 +411,9 @@ direction::
// ...
class ProductController extends AbstractController
{
public function showProducts(ManagerRegistry $doctrine, int $id): Response
public function showProducts(CategoryRepository $categoryRepository, int $id): Response
{
$category = $doctrine->getRepository(Category::class)->find($id);
$category = $categoryRepository->find($id);

$products = $category->getProducts();

Expand All @@ -433,7 +432,7 @@ by adding JOINs.
a "proxy" object in place of the true object. Look again at the above
example::

$product = $doctrine->getRepository(Product::class)->find($id);
$product = $productRepository->find($id);

$category = $product->getCategory();

Expand Down Expand Up @@ -503,9 +502,9 @@ object and its related ``Category`` in one query::
// ...
class ProductController extends AbstractController
{
public function show(ManagerRegistry $doctrine, int $id): Response
public function show(ProductRepository $productRepository, int $id): Response
{
$product = $doctrine->getRepository(Product::class)->findOneByIdJoinedToCategory($id);
$product = $productRepository->findOneByIdJoinedToCategory($id);

$category = $product->getCategory();

Expand Down