Skip to content

Commit ad76e3c

Browse files
committed
Merge branch '6.1' into 6.2
* 6.1: [Doctrine] show attributes instead of annotations for maker-bundle
2 parents 0252b2e + 8056fec commit ad76e3c

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

doctrine.rst

+30-42
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,19 @@ Whoa! You now have a new ``src/Entity/Product.php`` file::
140140
use App\Repository\ProductRepository;
141141
use Doctrine\ORM\Mapping as ORM;
142142

143-
/**
144-
* @ORM\Entity(repositoryClass=ProductRepository::class)
145-
*/
143+
#[ORM\Entity(repositoryClass: ProductRepository::class)]
146144
class Product
147145
{
148-
/**
149-
* @ORM\Id()
150-
* @ORM\GeneratedValue()
151-
* @ORM\Column(type="integer")
152-
*/
153-
private $id;
146+
#[ORM\Id]
147+
#[ORM\GeneratedValue]
148+
#[ORM\Column]
149+
private int $id;
154150

155-
/**
156-
* @ORM\Column(type="string", length=255)
157-
*/
158-
private $name;
151+
#[ORM\Column(length: 255)]
152+
private string $name;
159153

160-
/**
161-
* @ORM\Column(type="integer")
162-
*/
163-
private $price;
154+
#[ORM\Column]
155+
private int $price;
164156

165157
public function getId(): ?int
166158
{
@@ -170,6 +162,10 @@ Whoa! You now have a new ``src/Entity/Product.php`` file::
170162
// ... getter and setter methods
171163
}
172164

165+
.. note::
166+
167+
Starting in v1.44.0 - MakerBundle only supports entities using PHP attributes.
168+
173169
.. note::
174170

175171
Confused why the price is an integer? Don't worry: this is just an example.
@@ -194,8 +190,8 @@ Whoa! You now have a new ``src/Entity/Product.php`` file::
194190

195191
This class is called an "entity". And soon, you'll be able to save and query Product
196192
objects to a ``product`` table in your database. Each property in the ``Product``
197-
entity can be mapped to a column in that table. This is usually done with annotations:
198-
the ``@ORM\...`` comments that you see above each property:
193+
entity can be mapped to a column in that table. This is usually done with attributes:
194+
the ``#[ORM\Column(...)]`` comments that you see above each property:
199195

200196
.. image:: /_images/doctrine/mapping_single_entity.png
201197
:align: center
@@ -214,8 +210,8 @@ If you want to use XML instead of annotations, add ``type: xml`` and
214210
Be careful not to use reserved SQL keywords as your table or column names
215211
(e.g. ``GROUP`` or ``USER``). See Doctrine's `Reserved SQL keywords documentation`_
216212
for details on how to escape these. Or, change the table name with
217-
``@ORM\Table(name="groups")`` above the class or configure the column name with
218-
the ``name="group_name"`` option.
213+
``#[ORM\Table(name: "groups")]`` above the class or configure the column name with
214+
the ``name: "group_name"`` option.
219215

220216
.. _doctrine-creating-the-database-tables-schema:
221217

@@ -292,9 +288,7 @@ methods:
292288
{
293289
// ...
294290
295-
+ /**
296-
+ * @ORM\Column(type="text")
297-
+ */
291+
+ #[ORM\Column(type: 'text')]
298292
+ private $description;
299293
300294
// getDescription() & setDescription() were also added
@@ -363,12 +357,11 @@ and save it::
363357
use App\Entity\Product;
364358
use Doctrine\Persistence\ManagerRegistry;
365359
use Symfony\Component\HttpFoundation\Response;
360+
use Symfony\Component\Routing\Annotation\Route;
366361

367362
class ProductController extends AbstractController
368363
{
369-
/**
370-
* @Route("/product", name="create_product")
371-
*/
364+
#[Route('/product', name: 'create_product')]
372365
public function createProduct(ManagerRegistry $doctrine): Response
373366
{
374367
$entityManager = $doctrine->getManager();
@@ -447,14 +440,13 @@ some basic validation tasks::
447440

448441
use App\Entity\Product;
449442
use Symfony\Component\HttpFoundation\Response;
443+
use Symfony\Component\Routing\Annotation\Route;
450444
use Symfony\Component\Validator\Validator\ValidatorInterface;
451445
// ...
452446

453447
class ProductController extends AbstractController
454448
{
455-
/**
456-
* @Route("/product", name="create_product")
457-
*/
449+
#[Route('/product', name: 'create_product')]
458450
public function createProduct(ValidatorInterface $validator): Response
459451
{
460452
$product = new Product();
@@ -513,13 +505,12 @@ be able to go to ``/product/1`` to see your new product::
513505

514506
use App\Entity\Product;
515507
use Symfony\Component\HttpFoundation\Response;
508+
use Symfony\Component\Routing\Annotation\Route;
516509
// ...
517510

518511
class ProductController extends AbstractController
519512
{
520-
/**
521-
* @Route("/product/{id}", name="product_show")
522-
*/
513+
#[Route('/product/{id}', name: 'product_show')]
523514
public function show(ManagerRegistry $doctrine, int $id): Response
524515
{
525516
$product = $doctrine->getRepository(Product::class)->find($id);
@@ -547,13 +538,12 @@ and injected by the dependency injection container::
547538
use App\Entity\Product;
548539
use App\Repository\ProductRepository;
549540
use Symfony\Component\HttpFoundation\Response;
541+
use Symfony\Component\Routing\Annotation\Route;
550542
// ...
551543

552544
class ProductController extends AbstractController
553545
{
554-
/**
555-
* @Route("/product/{id}", name="product_show")
556-
*/
546+
#[Route('/product/{id}', name: 'product_show')]
557547
public function show(int $id, ProductRepository $productRepository): Response
558548
{
559549
$product = $productRepository
@@ -631,13 +621,12 @@ Now, simplify your controller::
631621
use App\Entity\Product;
632622
use App\Repository\ProductRepository;
633623
use Symfony\Component\HttpFoundation\Response;
624+
use Symfony\Component\Routing\Annotation\Route;
634625
// ...
635626

636627
class ProductController extends AbstractController
637628
{
638-
/**
639-
* @Route("/product/{id}", name="product_show")
640-
*/
629+
#[Route('/product/{id}', name: 'product_show')]
641630
public function show(Product $product): Response
642631
{
643632
// use the Product!
@@ -662,13 +651,12 @@ with any PHP model::
662651
use App\Entity\Product;
663652
use App\Repository\ProductRepository;
664653
use Symfony\Component\HttpFoundation\Response;
654+
use Symfony\Component\Routing\Annotation\Route;
665655
// ...
666656

667657
class ProductController extends AbstractController
668658
{
669-
/**
670-
* @Route("/product/edit/{id}")
671-
*/
659+
#[Route('/product/edit/{id}', name: 'product_edit')]
672660
public function update(ManagerRegistry $doctrine, int $id): Response
673661
{
674662
$entityManager = $doctrine->getManager();

0 commit comments

Comments
 (0)