Skip to content

[Doctrine] update some doctrine documentation #17958

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
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
34 changes: 15 additions & 19 deletions doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,13 @@ methods:

// src/Entity/Product.php
// ...
+ use Doctrine\DBAL\Types\Types;

class Product
{
// ...

+ #[ORM\Column(type: 'text')]
+ #[ORM\Column(type: Types::TEXT)]
+ private $description;

// getDescription() & setDescription() were also added
Expand Down Expand Up @@ -350,17 +351,15 @@ and save it::

// ...
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: 'create_product')]
public function createProduct(ManagerRegistry $doctrine): Response
public function createProduct(EntityManagerInterface $entityManager): Response
{
$entityManager = $doctrine->getManager();

$product = new Product();
$product->setName('Keyboard');
$product->setPrice(1999);
Expand Down Expand Up @@ -394,13 +393,10 @@ Take a look at the previous example in more detail:

.. _doctrine-entity-manager:

* **line 13** The ``ManagerRegistry $doctrine`` argument tells Symfony to
:ref:`inject the Doctrine service <services-constructor-injection>` into the
controller method.

* **line 15** The ``$doctrine->getManager()`` method gets Doctrine's
*entity manager* object, which is the most important object in Doctrine. It's
responsible for saving objects to, and fetching objects from, the database.
* **line 13** The ``EntityManagerInterface $entityManager`` argument tells Symfony
to :ref:`inject the Entity Manager service <services-constructor-injection>` into
the controller method. This object is responsible for saving objects to, and
fetching objects from, the database.

* **lines 17-20** In this section, you instantiate and work with the ``$product``
object like any other normal PHP object.
Expand Down Expand Up @@ -499,16 +495,17 @@ be able to go to ``/product/1`` to see your new product::
namespace App\Controller;

use App\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
// ...

class ProductController extends AbstractController
{
#[Route('/product/{id}', name: 'product_show')]
public function show(ManagerRegistry $doctrine, int $id): Response
public function show(EntityManagerInterface $entityManager, int $id): Response
{
$product = $doctrine->getRepository(Product::class)->find($id);
$product = $entityManager->getRepository(Product::class)->find($id);

if (!$product) {
throw $this->createNotFoundException(
Expand Down Expand Up @@ -558,7 +555,7 @@ job is to help you fetch entities of a certain class.

Once you have a repository object, you have many helper methods::

$repository = $doctrine->getRepository(Product::class);
$repository = $entityManager->getRepository(Product::class);

// look for a single Product by its primary key (usually "id")
$product = $repository->find($id);
Expand Down Expand Up @@ -826,9 +823,8 @@ with any PHP model::
class ProductController extends AbstractController
{
#[Route('/product/edit/{id}', name: 'product_edit')]
public function update(ManagerRegistry $doctrine, int $id): Response
public function update(EntityManagerInterface $entityManager, int $id): Response
{
$entityManager = $doctrine->getManager();
$product = $entityManager->getRepository(Product::class)->find($id);

if (!$product) {
Expand Down Expand Up @@ -877,7 +873,7 @@ You've already seen how the repository object allows you to run basic queries
without any work::

// from inside a controller
$repository = $doctrine->getRepository(Product::class);
$repository = $entityManager->getRepository(Product::class);
$product = $repository->find($id);

But what if you need a more complex query? When you generated your entity with
Expand Down Expand Up @@ -944,7 +940,7 @@ Now, you can call this method on the repository::
// from inside a controller
$minPrice = 1000;

$products = $doctrine->getRepository(Product::class)->findAllGreaterThanPrice($minPrice);
$products = $entityManager->getRepository(Product::class)->findAllGreaterThanPrice($minPrice);

// ...

Expand Down