Skip to content

Commit 4e445fe

Browse files
committedOct 21, 2022
Migrate more documentation towards attributes
The previous rework missed some details / left the documentation in an inconsistent state. Searching for "annotation" case insensitively allows to find discrepancies and forgotten things.
1 parent d79e61f commit 4e445fe

25 files changed

+1167
-179
lines changed
 

‎docs/en/cookbook/advanced-field-value-conversion-using-custom-mapping-types.rst

+20-51
Original file line numberDiff line numberDiff line change
@@ -32,59 +32,39 @@ The entity class:
3232
3333
namespace Geo\Entity;
3434
35-
/**
36-
* @Entity
37-
*/
35+
use Geo\ValueObject\Point;
36+
37+
#[Entity]
3838
class Location
3939
{
40-
/**
41-
* @Column(type="point")
42-
*
43-
* @var \Geo\ValueObject\Point
44-
*/
45-
private $point;
46-
47-
/**
48-
* @Column(type="string")
49-
*
50-
* @var string
51-
*/
52-
private $address;
53-
54-
/**
55-
* @param \Geo\ValueObject\Point $point
56-
*/
57-
public function setPoint(\Geo\ValueObject\Point $point)
40+
#[Column(type: 'point')]
41+
private Point $point;
42+
43+
#[Column]
44+
private string $address;
45+
46+
public function setPoint(Point $point): void
5847
{
5948
$this->point = $point;
6049
}
6150
62-
/**
63-
* @return \Geo\ValueObject\Point
64-
*/
65-
public function getPoint()
51+
public function getPoint(): Point
6652
{
6753
return $this->point;
6854
}
6955
70-
/**
71-
* @param string $address
72-
*/
73-
public function setAddress($address)
56+
public function setAddress(string $address): void
7457
{
7558
$this->address = $address;
7659
}
7760
78-
/**
79-
* @return string
80-
*/
81-
public function getAddress()
61+
public function getAddress(): string
8262
{
8363
return $this->address;
8464
}
8565
}
8666
87-
We use the custom type ``point`` in the ``@Column`` docblock annotation of the
67+
We use the custom type ``point`` in the ``#[Column]`` attribute of the
8868
``$point`` field. We will create this custom mapping type in the next chapter.
8969

9070
The point class:
@@ -97,29 +77,18 @@ The point class:
9777
9878
class Point
9979
{
100-
101-
/**
102-
* @param float $latitude
103-
* @param float $longitude
104-
*/
105-
public function __construct($latitude, $longitude)
106-
{
107-
$this->latitude = $latitude;
108-
$this->longitude = $longitude;
80+
public function __construct(
81+
private float $latitude,
82+
private float $longitude,
83+
) {
10984
}
11085
111-
/**
112-
* @return float
113-
*/
114-
public function getLatitude()
86+
public function getLatitude(): float
11587
{
11688
return $this->latitude;
11789
}
11890
119-
/**
120-
* @return float
121-
*/
122-
public function getLongitude()
91+
public function getLongitude(): float
12392
{
12493
return $this->longitude;
12594
}

‎docs/en/cookbook/implementing-the-notify-changetracking-policy.rst

+5-7
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ implement the ``NotifyPropertyChanged`` interface from the
2929
<?php
3030
use Doctrine\Persistence\NotifyPropertyChanged;
3131
use Doctrine\Persistence\PropertyChangedListener;
32-
32+
3333
abstract class DomainObject implements NotifyPropertyChanged
3434
{
3535
private $listeners = array();
36-
36+
3737
public function addPropertyChangedListener(PropertyChangedListener $listener) {
3838
$this->listeners[] = $listener;
3939
}
40-
40+
4141
/** Notifies listeners of a change. */
4242
protected function onPropertyChanged($propName, $oldValue, $newValue) {
4343
if ($this->listeners) {
@@ -55,12 +55,12 @@ listeners:
5555
.. code-block:: php
5656
5757
<?php
58-
// Mapping not shown, either in annotations, xml or yaml as usual
58+
// Mapping not shown, either in attributes, annotations, xml or yaml as usual
5959
class MyEntity extends DomainObject
6060
{
6161
private $data;
6262
// ... other fields as usual
63-
63+
6464
public function setData($data) {
6565
if ($data != $this->data) { // check: is it actually modified?
6666
$this->onPropertyChanged('data', $this->data, $data);
@@ -73,5 +73,3 @@ The check whether the new value is different from the old one is
7373
not mandatory but recommended. That way you can avoid unnecessary
7474
updates and also have full control over when you consider a
7575
property changed.
76-
77-

‎docs/en/cookbook/strategy-cookbook-introduction.rst

+2-4
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ As you can see, we have a method "setBlockEntity" which ties a potential strateg
154154
* This var contains the classname of the strategy
155155
* that is used for this blockitem. (This string (!) value will be persisted by Doctrine ORM)
156156
*
157-
* This is a doctrine field, so make sure that you use an @column annotation or setup your
158-
* yaml or xml files correctly
157+
* This is a doctrine field, so make sure that you use a
158+
#[Column] attribute or setup your yaml or xml files correctly
159159
* @var string
160160
*/
161161
protected $strategyClassName;
@@ -251,5 +251,3 @@ This might look like this:
251251
252252
In this example, even some variables are set - like a view object
253253
or a specific configuration object.
254-
255-

‎docs/en/cookbook/validation-of-entities.rst

+20-8
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ are allowed to:
3636
public function assertCustomerAllowedBuying()
3737
{
3838
$orderLimit = $this->customer->getOrderLimit();
39-
39+
4040
$amount = 0;
4141
foreach ($this->orderLines as $line) {
4242
$amount += $line->getAmount();
4343
}
44-
44+
4545
if ($amount > $orderLimit) {
4646
throw new CustomerOrderLimitExceededException();
4747
}
@@ -53,7 +53,21 @@ code, enforcing it at any time is important so that customers with
5353
a unknown reputation don't owe your business too much money.
5454

5555
We can enforce this constraint in any of the metadata drivers.
56-
First Annotations:
56+
First Attributes:
57+
58+
.. code-block:: php
59+
60+
<?php
61+
62+
#[Entity]
63+
#[HasLifecycleCallbacks]
64+
class Order
65+
{
66+
#[PrePersist, PreUpdate]
67+
public function assertCustomerAllowedBuying() {}
68+
}
69+
70+
As Annotations:
5771

5872
.. code-block:: php
5973
@@ -98,19 +112,17 @@ validation callbacks.
98112
<?php
99113
class Order
100114
{
101-
/**
102-
* @PrePersist @PreUpdate
103-
*/
115+
#[PrePersist, PreUpdate]
104116
public function validate()
105117
{
106118
if (!($this->plannedShipDate instanceof DateTime)) {
107119
throw new ValidateException();
108120
}
109-
121+
110122
if ($this->plannedShipDate->format('U') < time()) {
111123
throw new ValidateException();
112124
}
113-
125+
114126
if ($this->customer == null) {
115127
throw new OrderRequiresCustomerException();
116128
}

‎docs/en/reference/advanced-configuration.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ steps of configuration.
1212
1313
use Doctrine\ORM\Configuration;
1414
use Doctrine\ORM\EntityManager;
15+
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
1516
use Doctrine\ORM\ORMSetup;
1617
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1718
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
@@ -28,7 +29,7 @@ steps of configuration.
2829
2930
$config = new Configuration;
3031
$config->setMetadataCache($metadataCache);
31-
$driverImpl = ORMSetup::createDefaultAnnotationDriver('/path/to/lib/MyProject/Entities');
32+
$driverImpl = new AttributeDriver(['/path/to/lib/MyProject/Entities']);
3233
$config->setMetadataDriverImpl($driverImpl);
3334
$config->setQueryCache($queryCache);
3435
$config->setProxyDir('/path/to/myproject/lib/MyProject/Proxies');
@@ -124,18 +125,17 @@ used in the examples. For information on the usage of the
124125
AnnotationDriver, XmlDriver or YamlDriver please refer to the dedicated
125126
chapters ``Annotation Reference``, ``XML Mapping`` and ``YAML Mapping``.
126127

127-
The annotation driver can be configured with a factory method on
128-
the ``Doctrine\ORM\Configuration``:
128+
The attribute driver can be injected in the ``Doctrine\ORM\Configuration``:
129129

130130
.. code-block:: php
131131
132132
<?php
133133
use Doctrine\ORM\ORMSetup;
134134
135-
$driverImpl = ORMSetup::createDefaultAnnotationDriver('/path/to/lib/MyProject/Entities');
135+
$driverImpl = new AttributeDriver(['/path/to/lib/MyProject/Entities']);
136136
$config->setMetadataDriverImpl($driverImpl);
137137
138-
The path information to the entities is required for the annotation
138+
The path information to the entities is required for the attribute
139139
driver, because otherwise mass-operations on all entities through
140140
the console could not work correctly. All of metadata drivers
141141
accept either a single directory as a string or an array of
@@ -152,7 +152,7 @@ Metadata Cache (***RECOMMENDED***)
152152
$config->getMetadataCache();
153153
154154
Gets or sets the cache adapter to use for caching metadata
155-
information, that is, all the information you supply via
155+
information, that is, all the information you supply via attributes,
156156
annotations, xml or yaml, so that they do not need to be parsed and
157157
loaded from scratch on every single request which is a waste of
158158
resources. The cache implementation must implement the PSR-6

0 commit comments

Comments
 (0)
Please sign in to comment.