Commit 2507352
committed
This PR was merged into the 1.0-dev branch.
Discussion
----------
fix(make:entity): handle new value if nullable in OneToOne association, close #195
Hi 👋
This PR is a proposal for #195 for handling the new value (if nullable) in a OneToOne association.
The following code was not valid, it fails at `$user->getUserProfile()` if we call `UserProfile#setUser(null)`:
```php
class UserProfile
{
// ...
public function setUser(?User $user): self
{
$this->user = $user;
// set (or unset) the owning side of the relation if necessary
$newUserProfile = null === $user ? null : $this;
if ($user->getUserProfile() !== $newUserProfile) {
$user->setUserProfile($newUserProfile);
}
return $this;
}
}
```
The following code takes care of:
- unset the user profile of the previous User (`$this->user`) if we assign a nullable User
- set the profile to the new user, and prevent an infinite loop with `user->getUserProfile() !== $this`
```php
class UserProfile
{
public function setUser(?User $user): self
{
// unset the owning side of the relation if necessary
if ($user === null && $this->user !== null) {
$this->user->setUserProfile(null);
}
// set the owning side of the relation if necessary
if ($user !== null && $user->getUserProfile() !== $this) {
$user->setUserProfile($this);
}
$this->user = $user;
return $this;
}
}
```
WDYT? Thanks!
Commits
-------
6f8ebae fix(make:entity): handle new value if nullable in OneToOne association, close #195
File tree
4 files changed
+80
-45
lines changed- src/Util
- tests
- Doctrine/fixtures/expected_overwrite/src/Entity
- Util/fixtures/add_one_to_one_relation
4 files changed
+80
-45
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
232 | 232 | | |
233 | 233 | | |
234 | 234 | | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
235 | 241 | | |
236 | 242 | | |
237 | 243 | | |
| |||
411 | 417 | | |
412 | 418 | | |
413 | 419 | | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | 420 | | |
422 | 421 | | |
423 | 422 | | |
| |||
538 | 537 | | |
539 | 538 | | |
540 | 539 | | |
541 | | - | |
542 | | - | |
543 | 540 | | |
544 | 541 | | |
545 | 542 | | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
546 | 549 | | |
547 | 550 | | |
548 | 551 | | |
| |||
1213 | 1216 | | |
1214 | 1217 | | |
1215 | 1218 | | |
| 1219 | + | |
1216 | 1220 | | |
1217 | 1221 | | |
1218 | 1222 | | |
1219 | 1223 | | |
1220 | 1224 | | |
1221 | 1225 | | |
1222 | | - | |
| 1226 | + | |
1223 | 1227 | | |
1224 | 1228 | | |
1225 | 1229 | | |
1226 | | - | |
1227 | | - | |
1228 | | - | |
1229 | | - | |
1230 | | - | |
1231 | | - | |
1232 | | - | |
1233 | | - | |
1234 | | - | |
1235 | | - | |
1236 | | - | |
1237 | | - | |
1238 | | - | |
1239 | | - | |
1240 | | - | |
1241 | | - | |
1242 | | - | |
1243 | | - | |
1244 | | - | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
1245 | 1233 | | |
1246 | | - | |
| 1234 | + | |
1247 | 1235 | | |
1248 | | - | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
1249 | 1243 | | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
1250 | 1256 | | |
1251 | | - | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
1252 | 1277 | | |
1253 | 1278 | | |
1254 | 1279 | | |
1255 | 1280 | | |
1256 | | - | |
| 1281 | + | |
1257 | 1282 | | |
1258 | 1283 | | |
1259 | 1284 | | |
| 1285 | + | |
| 1286 | + | |
1260 | 1287 | | |
1261 | 1288 | | |
1262 | 1289 | | |
| |||
Lines changed: 9 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
55 | 58 | | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
60 | 62 | | |
61 | 63 | | |
| 64 | + | |
| 65 | + | |
62 | 66 | | |
63 | 67 | | |
64 | 68 | | |
| |||
Lines changed: 9 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
37 | 40 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
42 | 44 | | |
43 | 45 | | |
| 46 | + | |
| 47 | + | |
44 | 48 | | |
45 | 49 | | |
46 | 50 | | |
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
38 | 36 | | |
39 | 37 | | |
40 | 38 | | |
41 | 39 | | |
42 | 40 | | |
| 41 | + | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
0 commit comments