@@ -36,71 +36,50 @@ Our entities look like:
36
36
namespace Bank\Entities;
37
37
38
38
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]
43
43
class Account
44
44
{
45
- /**
46
- * @ORM\Id
47
- * @ORM\GeneratedValue
48
- * @ORM\Column(type="integer")
49
- */
45
+ #[ORM\Id]
46
+ #[ORM\GeneratedValue]
47
+ #[ORM\Column(type: 'integer')]
50
48
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();
72
62
}
73
63
}
74
-
75
- /**
76
- * @ORM\Entity
77
- */
64
+
65
+ #[ORM\Entity]
78
66
class Entry
79
67
{
80
- /**
81
- * @ORM\Id
82
- * @ORM\GeneratedValue
83
- * @ORM\Column(type="integer")
84
- */
68
+ #[ORM\Id]
69
+ #[ORM\GeneratedValue]
70
+ #[ORM\Column(type: 'integer')]
85
71
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
+ ) {
101
80
// more stuff here, from/to whom, stated reason, execution date and such
102
81
}
103
-
82
+
104
83
public function getAmount(): Amount
105
84
{
106
85
return $this->amount;
@@ -193,9 +172,8 @@ relation with this method:
193
172
public function addEntry(int $amount): void
194
173
{
195
174
$this->assertAcceptEntryAllowed($amount);
196
-
197
- $e = new Entry($this, $amount);
198
- $this->entries[] = $e;
175
+
176
+ $this->entries[] = new Entry($this, $amount);
199
177
}
200
178
}
201
179
@@ -213,18 +191,18 @@ Now look at the following test-code for our entities:
213
191
{
214
192
$account = new Account("123456", maxCredit: 200);
215
193
$this->assertEquals(0, $account->getBalance());
216
-
194
+
217
195
$account->addEntry(500);
218
196
$this->assertEquals(500, $account->getBalance());
219
-
197
+
220
198
$account->addEntry(-700);
221
199
$this->assertEquals(-200, $account->getBalance());
222
200
}
223
-
201
+
224
202
public function testExceedMaxLimit()
225
203
{
226
204
$account = new Account("123456", maxCredit: 200);
227
-
205
+
228
206
$this->expectException(Exception::class);
229
207
$account->addEntry(-1000);
230
208
}
@@ -285,22 +263,19 @@ entries collection) we want to add an aggregate field called
285
263
<?php
286
264
class Account
287
265
{
288
- /**
289
- * @ORM\Column(type="integer")
290
- */
266
+ #[ORM\Column(type: 'integer')]
291
267
private int $balance = 0;
292
-
268
+
293
269
public function getBalance(): int
294
270
{
295
271
return $this->balance;
296
272
}
297
-
273
+
298
274
public function addEntry(int $amount): void
299
275
{
300
276
$this->assertAcceptEntryAllowed($amount);
301
-
302
- $e = new Entry($this, $amount);
303
- $this->entries[] = $e;
277
+
278
+ $this->entries[] = new Entry($this, $amount);
304
279
$this->balance += $amount;
305
280
}
306
281
}
@@ -331,13 +306,13 @@ potentially lead to inconsistent state. See this example:
331
306
// The Account $accId has a balance of 0 and a max credit limit of 200:
332
307
// request 1 account
333
308
$account1 = $em->find(Account::class, $accId);
334
-
309
+
335
310
// request 2 account
336
311
$account2 = $em->find(Account::class, $accId);
337
-
312
+
338
313
$account1->addEntry(-200);
339
314
$account2->addEntry(-200);
340
-
315
+
341
316
// now request 1 and 2 both flush the changes.
342
317
343
318
The aggregate field ``Account::$balance `` is now -200, however the
@@ -357,10 +332,8 @@ Optimistic locking is as easy as adding a version column:
357
332
358
333
class Account
359
334
{
360
- /**
361
- * @ORM\Column(type="integer")
362
- * @ORM\Version
363
- */
335
+ #[ORM\Column(type: 'integer')]
336
+ #[ORM\Version]
364
337
private int $version;
365
338
}
366
339
0 commit comments