@@ -140,27 +140,19 @@ Whoa! You now have a new ``src/Entity/Product.php`` file::
140
140
use App\Repository\ProductRepository;
141
141
use Doctrine\ORM\Mapping as ORM;
142
142
143
- /**
144
- * @ORM\Entity(repositoryClass=ProductRepository::class)
145
- */
143
+ #[ORM\Entity(repositoryClass: ProductRepository::class)]
146
144
class Product
147
145
{
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;
154
150
155
- /**
156
- * @ORM\Column(type="string", length=255)
157
- */
158
- private $name;
151
+ #[ORM\Column(length: 255)]
152
+ private string $name;
159
153
160
- /**
161
- * @ORM\Column(type="integer")
162
- */
163
- private $price;
154
+ #[ORM\Column]
155
+ private int $price;
164
156
165
157
public function getId(): ?int
166
158
{
@@ -170,6 +162,10 @@ Whoa! You now have a new ``src/Entity/Product.php`` file::
170
162
// ... getter and setter methods
171
163
}
172
164
165
+ .. note ::
166
+
167
+ Starting in v1.44.0 - MakerBundle only supports entities using PHP attributes.
168
+
173
169
.. note ::
174
170
175
171
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::
194
190
195
191
This class is called an "entity". And soon, you'll be able to save and query Product
196
192
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:
199
195
200
196
.. image :: /_images/doctrine/mapping_single_entity.png
201
197
:align: center
@@ -214,8 +210,8 @@ If you want to use XML instead of annotations, add ``type: xml`` and
214
210
Be careful not to use reserved SQL keywords as your table or column names
215
211
(e.g. ``GROUP `` or ``USER ``). See Doctrine's `Reserved SQL keywords documentation `_
216
212
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.
219
215
220
216
.. _doctrine-creating-the-database-tables-schema :
221
217
@@ -292,9 +288,7 @@ methods:
292
288
{
293
289
// ...
294
290
295
- + /**
296
- + * @ORM\Column(type="text")
297
- + */
291
+ + #[ORM\Column(type: 'text')]
298
292
+ private $description;
299
293
300
294
// getDescription() & setDescription() were also added
@@ -363,12 +357,11 @@ and save it::
363
357
use App\Entity\Product;
364
358
use Doctrine\Persistence\ManagerRegistry;
365
359
use Symfony\Component\HttpFoundation\Response;
360
+ use Symfony\Component\Routing\Annotation\Route;
366
361
367
362
class ProductController extends AbstractController
368
363
{
369
- /**
370
- * @Route("/product", name="create_product")
371
- */
364
+ #[Route('/product', name: 'create_product')]
372
365
public function createProduct(ManagerRegistry $doctrine): Response
373
366
{
374
367
$entityManager = $doctrine->getManager();
@@ -447,14 +440,13 @@ some basic validation tasks::
447
440
448
441
use App\Entity\Product;
449
442
use Symfony\Component\HttpFoundation\Response;
443
+ use Symfony\Component\Routing\Annotation\Route;
450
444
use Symfony\Component\Validator\Validator\ValidatorInterface;
451
445
// ...
452
446
453
447
class ProductController extends AbstractController
454
448
{
455
- /**
456
- * @Route("/product", name="create_product")
457
- */
449
+ #[Route('/product', name: 'create_product')]
458
450
public function createProduct(ValidatorInterface $validator): Response
459
451
{
460
452
$product = new Product();
@@ -513,13 +505,12 @@ be able to go to ``/product/1`` to see your new product::
513
505
514
506
use App\Entity\Product;
515
507
use Symfony\Component\HttpFoundation\Response;
508
+ use Symfony\Component\Routing\Annotation\Route;
516
509
// ...
517
510
518
511
class ProductController extends AbstractController
519
512
{
520
- /**
521
- * @Route("/product/{id}", name="product_show")
522
- */
513
+ #[Route('/product/{id}', name: 'product_show')]
523
514
public function show(ManagerRegistry $doctrine, int $id): Response
524
515
{
525
516
$product = $doctrine->getRepository(Product::class)->find($id);
@@ -547,13 +538,12 @@ and injected by the dependency injection container::
547
538
use App\Entity\Product;
548
539
use App\Repository\ProductRepository;
549
540
use Symfony\Component\HttpFoundation\Response;
541
+ use Symfony\Component\Routing\Annotation\Route;
550
542
// ...
551
543
552
544
class ProductController extends AbstractController
553
545
{
554
- /**
555
- * @Route("/product/{id}", name="product_show")
556
- */
546
+ #[Route('/product/{id}', name: 'product_show')]
557
547
public function show(int $id, ProductRepository $productRepository): Response
558
548
{
559
549
$product = $productRepository
@@ -631,13 +621,12 @@ Now, simplify your controller::
631
621
use App\Entity\Product;
632
622
use App\Repository\ProductRepository;
633
623
use Symfony\Component\HttpFoundation\Response;
624
+ use Symfony\Component\Routing\Annotation\Route;
634
625
// ...
635
626
636
627
class ProductController extends AbstractController
637
628
{
638
- /**
639
- * @Route("/product/{id}", name="product_show")
640
- */
629
+ #[Route('/product/{id}', name: 'product_show')]
641
630
public function show(Product $product): Response
642
631
{
643
632
// use the Product!
@@ -662,13 +651,12 @@ with any PHP model::
662
651
use App\Entity\Product;
663
652
use App\Repository\ProductRepository;
664
653
use Symfony\Component\HttpFoundation\Response;
654
+ use Symfony\Component\Routing\Annotation\Route;
665
655
// ...
666
656
667
657
class ProductController extends AbstractController
668
658
{
669
- /**
670
- * @Route("/product/edit/{id}")
671
- */
659
+ #[Route('/product/edit/{id}', name: 'product_edit')]
672
660
public function update(ManagerRegistry $doctrine, int $id): Response
673
661
{
674
662
$entityManager = $doctrine->getManager();
0 commit comments