diff --git a/components/uid.rst b/components/uid.rst index d81e593f812..affc484f3d2 100644 --- a/components/uid.rst +++ b/components/uid.rst @@ -133,14 +133,10 @@ type, which converts to/from UUID objects automatically:: use Doctrine\ORM\Mapping as ORM; - /** - * @ORM\Entity(repositoryClass="App\Repository\ProductRepository") - */ + #[ORM\Entity(repositoryClass: ProductRepository::class)] class Product { - /** - * @ORM\Column(type="uuid") - */ + #[ORM\Column(type: 'uuid')] private $someProperty; // ... @@ -156,12 +152,10 @@ entity primary keys:: class User implements UserInterface { - /** - * @ORM\Id - * @ORM\Column(type="uuid", unique=true) - * @ORM\GeneratedValue(strategy="CUSTOM") - * @ORM\CustomIdGenerator(class="doctrine.uuid_generator") - */ + #[ORM\Id] + #[ORM\Column(type: 'uuid', unique: true)] + #[ORM\GeneratedValue(strategy: 'CUSTOM')] + #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')] private $id; public function getId(): ?Uuid @@ -291,14 +285,10 @@ type, which converts to/from ULID objects automatically:: use Doctrine\ORM\Mapping as ORM; - /** - * @ORM\Entity(repositoryClass="App\Repository\ProductRepository") - */ + #[ORM\Entity(repositoryClass: ProductRepository::class)] class Product { - /** - * @ORM\Column(type="ulid") - */ + #[ORM\Column(type: 'ulid')] private $someProperty; // ... @@ -314,12 +304,10 @@ entity primary keys:: class Product { - /** - * @ORM\Id - * @ORM\Column(type="ulid", unique=true) - * @ORM\GeneratedValue(strategy="CUSTOM") - * @ORM\CustomIdGenerator(class="doctrine.ulid_generator") - */ + #[ORM\Id] + #[ORM\Column(type: 'ulid', unique: true)] + #[ORM\GeneratedValue(strategy: 'CUSTOM')] + #[ORM\CustomIdGenerator(class: 'doctrine.ulid_generator')] private $id; public function getId(): ?Ulid diff --git a/controller/upload_file.rst b/controller/upload_file.rst index b962710ca8f..158e7167a0b 100644 --- a/controller/upload_file.rst +++ b/controller/upload_file.rst @@ -24,9 +24,7 @@ add a PDF brochure for each product. To do so, add a new property called { // ... - /** - * @ORM\Column(type="string") - */ + #[ORM\Column(type: 'string')] private $brochureFilename; public function getBrochureFilename() diff --git a/doctrine.rst b/doctrine.rst index c821a5dff22..7c9a73d39dc 100644 --- a/doctrine.rst +++ b/doctrine.rst @@ -205,8 +205,8 @@ If you want to use XML instead of annotations, add ``type: xml`` and Be careful not to use reserved SQL keywords as your table or column names (e.g. ``GROUP`` or ``USER``). See Doctrine's `Reserved SQL keywords documentation`_ for details on how to escape these. Or, change the table name with - ``#[ORM\Table(name: "groups")]`` above the class or configure the column name with - the ``name: "group_name"`` option. + ``#[ORM\Table(name: 'groups')]`` above the class or configure the column name with + the ``name: 'group_name'`` option. .. _doctrine-creating-the-database-tables-schema: diff --git a/doctrine/associations.rst b/doctrine/associations.rst index b1045fd3e9d..8ebdadf7864 100644 --- a/doctrine/associations.rst +++ b/doctrine/associations.rst @@ -140,34 +140,6 @@ the ``Product`` entity (and getter & setter methods): .. configuration-block:: - .. code-block:: php-annotations - - // src/Entity/Product.php - namespace App\Entity; - - // ... - class Product - { - // ... - - /** - * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="products") - */ - private $category; - - public function getCategory(): ?Category - { - return $this->category; - } - - public function setCategory(?Category $category): self - { - $this->category = $category; - - return $this; - } - } - .. code-block:: php-attributes // src/Entity/Product.php @@ -178,7 +150,7 @@ the ``Product`` entity (and getter & setter methods): { // ... - #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: "products")] + #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'products')] private $category; public function getCategory(): ?Category @@ -237,40 +209,6 @@ class that will hold these objects: .. configuration-block:: - .. code-block:: php-annotations - - // src/Entity/Category.php - namespace App\Entity; - - // ... - use Doctrine\Common\Collections\ArrayCollection; - use Doctrine\Common\Collections\Collection; - - class Category - { - // ... - - /** - * @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="category") - */ - private $products; - - public function __construct() - { - $this->products = new ArrayCollection(); - } - - /** - * @return Collection|Product[] - */ - public function getProducts(): Collection - { - return $this->products; - } - - // addProduct() and removeProduct() were also added - } - .. code-block:: php-attributes // src/Entity/Category.php @@ -284,7 +222,7 @@ class that will hold these objects: { // ... - #[ORM\OneToMany(targetEntity: Product::class, mappedBy: "category")] + #[ORM\OneToMany(targetEntity: Product::class, mappedBy: 'category')] private $products; public function __construct() @@ -647,24 +585,13 @@ that behavior, use the `orphanRemoval`_ option inside ``Category``: .. configuration-block:: - .. code-block:: php-annotations - - // src/Entity/Category.php - - // ... - - /** - * @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="category", orphanRemoval=true) - */ - private $products; - .. code-block:: php-attributes // src/Entity/Category.php // ... - #[ORM\OneToMany(targetEntity: Product::class, mappedBy: "category", orphanRemoval: true)] + #[ORM\OneToMany(targetEntity: Product::class, mappedBy: 'category', orphanRemoval: true)] private $products; @@ -681,8 +608,8 @@ Doctrine's `Association Mapping Documentation`_. .. note:: - If you're using annotations, you'll need to prepend all annotations with - ``@ORM\`` (e.g. ``@ORM\OneToMany``), which is not reflected in Doctrine's + If you're using attributes, you'll need to prepend all attributes with + ``#[ORM\]`` (e.g. ``#[ORM\OneToMany]``), which is not reflected in Doctrine's documentation. .. _`Association Mapping Documentation`: https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/association-mapping.html diff --git a/doctrine/events.rst b/doctrine/events.rst index e64104f88f4..e29cd82c599 100644 --- a/doctrine/events.rst +++ b/doctrine/events.rst @@ -53,33 +53,6 @@ define a callback for the ``prePersist`` Doctrine event: .. configuration-block:: - .. code-block:: php-annotations - - // src/Entity/Product.php - namespace App\Entity; - - use Doctrine\ORM\Mapping as ORM; - - // When using annotations, don't forget to add @ORM\HasLifecycleCallbacks() - // to the class of the entity where you define the callback - - /** - * @ORM\Entity() - * @ORM\HasLifecycleCallbacks() - */ - class Product - { - // ... - - /** - * @ORM\PrePersist - */ - public function setCreatedAtValue(): void - { - $this->createdAt = new \DateTimeImmutable(); - } - } - .. code-block:: php-attributes // src/Entity/Product.php diff --git a/doctrine/resolve_target_entity.rst b/doctrine/resolve_target_entity.rst index 6c1569d411e..31186cc4046 100644 --- a/doctrine/resolve_target_entity.rst +++ b/doctrine/resolve_target_entity.rst @@ -46,10 +46,8 @@ A Customer entity:: use App\Model\InvoiceSubjectInterface; use Doctrine\ORM\Mapping as ORM; - /** - * @ORM\Entity - * @ORM\Table(name="customer") - */ + #[ORM\Entity] + #[ORM\Table(name: 'customer')] class Customer extends BaseCustomer implements InvoiceSubjectInterface { // In this example, any methods defined in the InvoiceSubjectInterface @@ -66,16 +64,15 @@ An Invoice entity:: /** * Represents an Invoice. - * - * @ORM\Entity - * @ORM\Table(name="invoice") */ + #[ORM\Entity] + #[ORM\Table(name: 'invoice')] class Invoice { /** - * @ORM\ManyToOne(targetEntity="App\Model\InvoiceSubjectInterface") * @var InvoiceSubjectInterface */ + #[ORM\ManyToOne(targetEntity: InvoiceSubjectInterface::class)] protected $subject; } diff --git a/form/form_collections.rst b/form/form_collections.rst index ca7dd6228f7..4d483d61159 100644 --- a/form/form_collections.rst +++ b/form/form_collections.rst @@ -407,15 +407,13 @@ you will learn about next!). .. configuration-block:: - .. code-block:: php-annotations + .. code-block:: php-attributes // src/Entity/Task.php // ... - /** - * @ORM\ManyToMany(targetEntity="App\Entity\Tag", cascade={"persist"}) - */ + #[ORM\ManyToMany(targetEntity: Tag::class, cascade: ['persist'])] protected $tags; .. code-block:: yaml diff --git a/quick_tour/flex_recipes.rst b/quick_tour/flex_recipes.rst index c4ab8af488d..c62a5230359 100644 --- a/quick_tour/flex_recipes.rst +++ b/quick_tour/flex_recipes.rst @@ -185,7 +185,7 @@ Security components, as well as the Doctrine ORM. In fact, Flex installed *5* re But like usual, we can immediately start using the new library. Want to create a rich API for a ``product`` table? Create a ``Product`` entity and give it the -``@ApiResource()`` annotation:: +``#[ApiResource]`` attribute:: books = new ArrayCollection(); - } - - // ... setter for name, adder and remover for books - - // the name can be validated by calling the getter - public function getName(): string - { - return $this->name; - } - - /** - * @return \Generator|Book[] The books for a given author - */ - public function getBooksForAuthor(Author $author): iterable - { - foreach ($this->books as $book) { - if ($book->isAuthoredBy($author)) { - yield $book; - } - } - } - - // neither the method above nor any other specific getter - // could be used to validated all nested books; - // this object needs to be traversed to call the iterator - public function getIterator() - { - return $this->books->getIterator(); - } - } - .. code-block:: php-attributes // src/Entity/BookCollection.php @@ -112,7 +44,7 @@ that all have constraints on their properties. /** * @var Collection|Book[] */ - #[ORM\ManyToMany(targetEntity: Book::class)] + #[ORM\ManyToMany(targetEntity: Book::class)] protected $books; // some other properties diff --git a/reference/constraints/UniqueEntity.rst b/reference/constraints/UniqueEntity.rst index 7ba0ddf2a1e..6ea04862fba 100644 --- a/reference/constraints/UniqueEntity.rst +++ b/reference/constraints/UniqueEntity.rst @@ -30,31 +30,6 @@ between all of the rows in your user table: .. configuration-block:: - .. code-block:: php-annotations - - // src/Entity/User.php - namespace App\Entity; - - use Doctrine\ORM\Mapping as ORM; - - // DON'T forget the following use statement!!! - use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; - - use Symfony\Component\Validator\Constraints as Assert; - - /** - * @ORM\Entity - * @UniqueEntity("email") - */ - class User - { - /** - * @ORM\Column(name="email", type="string", length=255, unique=true) - * @Assert\Email - */ - protected $email; - } - .. code-block:: php-attributes // src/Entity/User.php @@ -176,35 +151,6 @@ Consider this example: .. configuration-block:: - .. code-block:: php-annotations - - // src/Entity/Service.php - namespace App\Entity; - - use Doctrine\ORM\Mapping as ORM; - use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; - - /** - * @ORM\Entity - * @UniqueEntity( - * fields={"host", "port"}, - * errorPath="port", - * message="This port is already in use on that host." - * ) - */ - class Service - { - /** - * @ORM\ManyToOne(targetEntity="App\Entity\Host") - */ - public $host; - - /** - * @ORM\Column(type="integer") - */ - public $port; - } - .. code-block:: php-attributes // src/Entity/Service.php diff --git a/security.rst b/security.rst index fd1d6a4b96b..cc99505d152 100644 --- a/security.rst +++ b/security.rst @@ -118,32 +118,21 @@ from the `MakerBundle`_: use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; - /** - * @ORM\Entity(repositoryClass=UserRepository::class) - */ + #[ORM\Entity(repositoryClass: UserRepository::class)] class User implements UserInterface, PasswordAuthenticatedUserInterface { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string", length=180, unique=true) - */ + #[ORM\Column(type: 'string', length: 180, unique: true)] private $email; - /** - * @ORM\Column(type="json") - */ + #[ORM\Column(type: 'json')] private $roles = []; - /** - * @var string The hashed password - * @ORM\Column(type="string") - */ + #[ORM\Column(type: 'string')] private $password; public function getId(): ?int @@ -1823,9 +1812,7 @@ database and every user is *always* given at least one role: ``ROLE_USER``:: // ... class User { - /** - * @ORM\Column(type="json") - */ + #[ORM\Column(type: 'json')] private $roles = []; // ...