Commit 31c3b33
committed
bug symfony#61036 [ObjectMapper] Correctly manage constructor initialization (alanpoulain)
This PR was merged into the 7.3 branch.
Discussion
----------
[ObjectMapper] Correctly manage constructor initialization
| Q | A
| ------------- | ---
| Branch? | 7.3
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Issues | Fix symfony#60797 Fix symfony#60807
| License | MIT
Currently the ObjectMapper cannot support empty constructor initialization, which is particularly useful for Doctrine collections:
```php
class Article
{
#[ORM\ManyToMany(targetEntity: Tag::class)]
private Collection $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
public function addTag($tag)
{
// ...
}
public function removeTag($tag)
{
// ...
}
}
```
This kind of error is thrown by the PropertyAccessor:
> Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException: The property "Symfony\Component\ObjectMapper\Tests\Fixtures\InitializedConstructor\B::$tags" is not readable because it is typed "array". You should initialize it or declare a default value instead.
This PR fixes this but also another related issue (symfony#60807): since an "empty" (no constructor arguments) constructor is always called, the class-level transformation test does not pass anymore:
> Symfony\Component\ObjectMapper\Tests\ObjectMapperTest::testMapToWithInstanceHook
ArgumentCountError: Too few arguments to function Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallback\B::__construct(), 0 passed in /home/workspace/symfony/symfony/src/Symfony/Component/ObjectMapper/ObjectMapper.php on line 151 and exactly 1 expected
To solve this, a check is done on `map?->transform`.
Commits
-------
38e4037 [ObjectMapper] Correctly manage constructor initializationFile tree
4 files changed
+60
-1
lines changed- src/Symfony/Component/ObjectMapper
- Tests
- Fixtures/InitializedConstructor
4 files changed
+60
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | | - | |
| 149 | + | |
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
| |||
Lines changed: 17 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
Lines changed: 31 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| |||
147 | 149 | | |
148 | 150 | | |
149 | 151 | | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
150 | 161 | | |
151 | 162 | | |
152 | 163 | | |
| |||
0 commit comments