Skip to content

Replace ORM annotations with attributes, remove ORM annotation configuration block #17155

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
Aug 11, 2022
Merged
Show file tree
Hide file tree
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
36 changes: 12 additions & 24 deletions components/uid.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// ...
Expand All @@ -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
Expand Down Expand Up @@ -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;

// ...
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
83 changes: 5 additions & 78 deletions doctrine/associations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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;


Expand All @@ -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
Expand Down
27 changes: 0 additions & 27 deletions doctrine/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions doctrine/resolve_target_entity.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
6 changes: 2 additions & 4 deletions form/form_collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 8 additions & 16 deletions quick_tour/flex_recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

<?php
// src/Entity/Product.php
Expand All @@ -194,27 +194,19 @@ rich API for a ``product`` table? Create a ``Product`` entity and give it the
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ApiResource()
*/
#[ORM\Entity]
#[ApiResource]
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
private $id;

/**
* @ORM\Column(type="string")
*/
#[ORM\Column(type: 'string')]
private $name;

/**
* @ORM\Column(type="int")
*/
#[ORM\Column(type: 'integer')]
private $price;

// ...
Expand Down
Loading