Skip to content

Commit ac24c11

Browse files
authored
Modernize code in documentation (#11179)
Somehow, there still were code samples relying on annotations.
1 parent dd478d8 commit ac24c11

File tree

2 files changed

+56
-92
lines changed

2 files changed

+56
-92
lines changed

docs/en/cookbook/aggregate-fields.rst

Lines changed: 50 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -36,71 +36,50 @@ Our entities look like:
3636
namespace Bank\Entities;
3737
3838
use Doctrine\ORM\Mapping as ORM;
39-
40-
/**
41-
* @ORM\Entity
42-
*/
39+
use Doctrine\Common\Collections\ArrayCollection;
40+
use Doctrine\Common\Collections\Collection;
41+
42+
#[ORM\Entity]
4343
class Account
4444
{
45-
/**
46-
* @ORM\Id
47-
* @ORM\GeneratedValue
48-
* @ORM\Column(type="integer")
49-
*/
45+
#[ORM\Id]
46+
#[ORM\GeneratedValue]
47+
#[ORM\Column(type: 'integer')]
5048
private ?int $id;
51-
52-
/**
53-
* @ORM\Column(type="string", unique=true)
54-
*/
55-
private string $no;
56-
57-
/**
58-
* @ORM\OneToMany(targetEntity="Entry", mappedBy="account", cascade={"persist"})
59-
*/
60-
private array $entries;
61-
62-
/**
63-
* @ORM\Column(type="integer")
64-
*/
65-
private int $maxCredit = 0;
66-
67-
public function __construct(string $no, int $maxCredit = 0)
68-
{
69-
$this->no = $no;
70-
$this->maxCredit = $maxCredit;
71-
$this->entries = new \Doctrine\Common\Collections\ArrayCollection();
49+
50+
#[ORM\OneToMany(targetEntity: Entry::class, mappedBy: 'account', cascade: ['persist'])]
51+
private Collection $entries;
52+
53+
54+
public function __construct(
55+
#[ORM\Column(type: 'string', unique: true)]
56+
private string $no,
57+
58+
#[ORM\Column(type: 'integer')]
59+
private int $maxCredit = 0,
60+
) {
61+
$this->entries = new ArrayCollection();
7262
}
7363
}
74-
75-
/**
76-
* @ORM\Entity
77-
*/
64+
65+
#[ORM\Entity]
7866
class Entry
7967
{
80-
/**
81-
* @ORM\Id
82-
* @ORM\GeneratedValue
83-
* @ORM\Column(type="integer")
84-
*/
68+
#[ORM\Id]
69+
#[ORM\GeneratedValue]
70+
#[ORM\Column(type: 'integer')]
8571
private ?int $id;
86-
87-
/**
88-
* @ORM\ManyToOne(targetEntity="Account", inversedBy="entries")
89-
*/
90-
private Account $account;
91-
92-
/**
93-
* @ORM\Column(type="integer")
94-
*/
95-
private int $amount;
96-
97-
public function __construct(Account $account, int $amount)
98-
{
99-
$this->account = $account;
100-
$this->amount = $amount;
72+
73+
public function __construct(
74+
#[ORM\ManyToOne(targetEntity: Account::class, inversedBy: 'entries')]
75+
private Account $account,
76+
77+
#[ORM\Column(type: 'integer')]
78+
private int $amount,
79+
) {
10180
// more stuff here, from/to whom, stated reason, execution date and such
10281
}
103-
82+
10483
public function getAmount(): Amount
10584
{
10685
return $this->amount;
@@ -193,9 +172,8 @@ relation with this method:
193172
public function addEntry(int $amount): void
194173
{
195174
$this->assertAcceptEntryAllowed($amount);
196-
197-
$e = new Entry($this, $amount);
198-
$this->entries[] = $e;
175+
176+
$this->entries[] = new Entry($this, $amount);
199177
}
200178
}
201179
@@ -213,18 +191,18 @@ Now look at the following test-code for our entities:
213191
{
214192
$account = new Account("123456", maxCredit: 200);
215193
$this->assertEquals(0, $account->getBalance());
216-
194+
217195
$account->addEntry(500);
218196
$this->assertEquals(500, $account->getBalance());
219-
197+
220198
$account->addEntry(-700);
221199
$this->assertEquals(-200, $account->getBalance());
222200
}
223-
201+
224202
public function testExceedMaxLimit()
225203
{
226204
$account = new Account("123456", maxCredit: 200);
227-
205+
228206
$this->expectException(Exception::class);
229207
$account->addEntry(-1000);
230208
}
@@ -285,22 +263,19 @@ entries collection) we want to add an aggregate field called
285263
<?php
286264
class Account
287265
{
288-
/**
289-
* @ORM\Column(type="integer")
290-
*/
266+
#[ORM\Column(type: 'integer')]
291267
private int $balance = 0;
292-
268+
293269
public function getBalance(): int
294270
{
295271
return $this->balance;
296272
}
297-
273+
298274
public function addEntry(int $amount): void
299275
{
300276
$this->assertAcceptEntryAllowed($amount);
301-
302-
$e = new Entry($this, $amount);
303-
$this->entries[] = $e;
277+
278+
$this->entries[] = new Entry($this, $amount);
304279
$this->balance += $amount;
305280
}
306281
}
@@ -331,13 +306,13 @@ potentially lead to inconsistent state. See this example:
331306
// The Account $accId has a balance of 0 and a max credit limit of 200:
332307
// request 1 account
333308
$account1 = $em->find(Account::class, $accId);
334-
309+
335310
// request 2 account
336311
$account2 = $em->find(Account::class, $accId);
337-
312+
338313
$account1->addEntry(-200);
339314
$account2->addEntry(-200);
340-
315+
341316
// now request 1 and 2 both flush the changes.
342317
343318
The aggregate field ``Account::$balance`` is now -200, however the
@@ -357,10 +332,8 @@ Optimistic locking is as easy as adding a version column:
357332
358333
class Account
359334
{
360-
/**
361-
* @ORM\Column(type="integer")
362-
* @ORM\Version
363-
*/
335+
#[ORM\Column(type: 'integer')]
336+
#[ORM\Version]
364337
private int $version;
365338
}
366339

docs/en/cookbook/resolve-target-entity-listener.rst

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ A Customer entity
4747
use Acme\CustomerModule\Entity\Customer as BaseCustomer;
4848
use Acme\InvoiceModule\Model\InvoiceSubjectInterface;
4949
50-
/**
51-
* @ORM\Entity
52-
* @ORM\Table(name="customer")
53-
*/
50+
#[ORM\Entity]
51+
#[ORM\Table(name: 'customer')]
5452
class Customer extends BaseCustomer implements InvoiceSubjectInterface
5553
{
5654
// In our example, any methods defined in the InvoiceSubjectInterface
@@ -69,19 +67,12 @@ An Invoice entity
6967
use Doctrine\ORM\Mapping AS ORM;
7068
use Acme\InvoiceModule\Model\InvoiceSubjectInterface;
7169
72-
/**
73-
* Represents an Invoice.
74-
*
75-
* @ORM\Entity
76-
* @ORM\Table(name="invoice")
77-
*/
70+
#[ORM\Entity]
71+
#[ORM\Table(name: 'invoice')]
7872
class Invoice
7973
{
80-
/**
81-
* @ORM\ManyToOne(targetEntity="Acme\InvoiceModule\Model\InvoiceSubjectInterface")
82-
* @var InvoiceSubjectInterface
83-
*/
84-
protected $subject;
74+
#[ORM\ManyToOne(targetEntity: InvoiceSubjectInterface::class)]
75+
protected InvoiceSubjectInterface $subject;
8576
}
8677
8778
An InvoiceSubjectInterface

0 commit comments

Comments
 (0)