Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 643f34f

Browse filesBrowse files
committed
bug #35794 [DoctrineBridge][DoctrineExtractor] Fix indexBy with custom and some core types (fancyweb)
This PR was merged into the 3.4 branch. Discussion ---------- [DoctrineBridge][DoctrineExtractor] Fix indexBy with custom and some core types | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #35542 and #35604 | License | MIT | Doc PR | - For #35604: To guess the collection key type, the `getPhpType()` method is called. But it does not handle most objects and arrays core types. This is why an indexBy datetime does not work. For #35542: When the php type cannot be guessed, null is returned. In this case, we cannot pass a valid builtin type to PropertyInfo Type, so we should return null. Commits ------- 018ec1a [DoctrineBridge][DoctrineExtractor] Fix indexBy with custom and some core types
2 parents 8197d9a + 018ec1a commit 643f34f
Copy full SHA for 643f34f

File tree

4 files changed

+85
-28
lines changed
Filter options

4 files changed

+85
-28
lines changed

‎src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
+52-28Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ public function getTypes($class, $property, array $context = [])
117117
$typeOfField = $subMetadata->getTypeOfField($indexProperty);
118118
}
119119

120-
$collectionKeyType = $this->getPhpType($typeOfField);
120+
if (!$collectionKeyType = $this->getPhpType($typeOfField)) {
121+
return null;
122+
}
121123
}
122124
}
123125

@@ -137,39 +139,46 @@ public function getTypes($class, $property, array $context = [])
137139

138140
if ($metadata->hasField($property)) {
139141
$typeOfField = $metadata->getTypeOfField($property);
140-
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
141-
142-
switch ($typeOfField) {
143-
case DBALType::DATE:
144-
case DBALType::DATETIME:
145-
case DBALType::DATETIMETZ:
146-
case 'vardatetime':
147-
case DBALType::TIME:
148-
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];
149142

150-
case 'date_immutable':
151-
case 'datetime_immutable':
152-
case 'datetimetz_immutable':
153-
case 'time_immutable':
154-
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTimeImmutable')];
155-
156-
case 'dateinterval':
157-
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateInterval')];
158-
159-
case DBALType::TARRAY:
160-
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
143+
if (!$builtinType = $this->getPhpType($typeOfField)) {
144+
return null;
145+
}
161146

162-
case DBALType::SIMPLE_ARRAY:
163-
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))];
147+
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
164148

165-
case DBALType::JSON_ARRAY:
166-
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
149+
switch ($builtinType) {
150+
case Type::BUILTIN_TYPE_OBJECT:
151+
switch ($typeOfField) {
152+
case DBALType::DATE:
153+
case DBALType::DATETIME:
154+
case DBALType::DATETIMETZ:
155+
case 'vardatetime':
156+
case DBALType::TIME:
157+
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];
158+
159+
case 'date_immutable':
160+
case 'datetime_immutable':
161+
case 'datetimetz_immutable':
162+
case 'time_immutable':
163+
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTimeImmutable')];
164+
165+
case 'dateinterval':
166+
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateInterval')];
167+
}
167168

168-
default:
169-
$builtinType = $this->getPhpType($typeOfField);
169+
break;
170+
case Type::BUILTIN_TYPE_ARRAY:
171+
switch ($typeOfField) {
172+
case DBALType::TARRAY:
173+
case DBALType::JSON_ARRAY:
174+
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
170175

171-
return $builtinType ? [new Type($builtinType, $nullable)] : null;
176+
case DBALType::SIMPLE_ARRAY:
177+
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))];
178+
}
172179
}
180+
181+
return [new Type($builtinType, $nullable)];
173182
}
174183

175184
return null;
@@ -234,7 +243,22 @@ private function getPhpType($doctrineType)
234243
return Type::BUILTIN_TYPE_RESOURCE;
235244

236245
case DBALType::OBJECT:
246+
case DBALType::DATE:
247+
case DBALType::DATETIME:
248+
case DBALType::DATETIMETZ:
249+
case 'vardatetime':
250+
case DBALType::TIME:
251+
case 'date_immutable':
252+
case 'datetime_immutable':
253+
case 'datetimetz_immutable':
254+
case 'time_immutable':
255+
case 'dateinterval':
237256
return Type::BUILTIN_TYPE_OBJECT;
257+
258+
case DBALType::TARRAY:
259+
case DBALType::SIMPLE_ARRAY:
260+
case DBALType::JSON_ARRAY:
261+
return Type::BUILTIN_TYPE_ARRAY;
238262
}
239263

240264
return null;

‎src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo;
1313

14+
use Doctrine\Common\Collections\Collection;
1415
use Doctrine\DBAL\Types\Type as DBALType;
1516
use Doctrine\ORM\EntityManager;
1617
use Doctrine\ORM\Tools\Setup;
1718
use PHPUnit\Framework\TestCase;
1819
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
20+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation;
1921
use Symfony\Component\PropertyInfo\Type;
2022

2123
/**
@@ -62,6 +64,8 @@ public function testGetProperties()
6264
'bar',
6365
'indexedBar',
6466
'indexedFoo',
67+
'indexedByDt',
68+
'indexedByCustomType',
6569
],
6670
$this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy')
6771
);
@@ -153,6 +157,15 @@ public function typesProvider()
153157
['simpleArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
154158
['customFoo', null],
155159
['notMapped', null],
160+
['indexedByDt', [new Type(
161+
Type::BUILTIN_TYPE_OBJECT,
162+
false,
163+
Collection::class,
164+
true,
165+
new Type(Type::BUILTIN_TYPE_OBJECT),
166+
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
167+
)]],
168+
['indexedByCustomType', null],
156169
];
157170
}
158171

‎src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineDummy.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineDummy.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,14 @@ class DoctrineDummy
112112
private $bigint;
113113

114114
public $notMapped;
115+
116+
/**
117+
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="dt", indexBy="dt")
118+
*/
119+
protected $indexedByDt;
120+
121+
/**
122+
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="customType", indexBy="customType")
123+
*/
124+
private $indexedByCustomType;
115125
}

‎src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineRelation.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineRelation.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,14 @@ class DoctrineRelation
3939
* @ManyToOne(targetEntity="DoctrineDummy", inversedBy="indexedFoo")
4040
*/
4141
protected $foo;
42+
43+
/**
44+
* @Column(type="datetime")
45+
*/
46+
private $dt;
47+
48+
/**
49+
* @Column(type="foo")
50+
*/
51+
private $customType;
4252
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.