Skip to content

Commit cdb60e2

Browse files
authored
Merge pull request #2444 from malarzm/type-autoconfigure-final-touches-mm
Final touches for type autoconfiguration
2 parents df1d775 + b274e9f commit cdb60e2

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

docs/en/reference/basic-mapping.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ This list explains some of the less obvious mapping types:
203203
PHP Types Mapping
204204
_________________
205205

206+
.. note::
207+
Doctrine will skip type autoconfiguration if settings are provided explicitly.
208+
206209
Since version 2.4 Doctrine can determine usable defaults from property types
207210
on document classes. Doctrine will map PHP types to ``type`` attribute as
208211
follows:
@@ -215,11 +218,16 @@ follows:
215218
- ``int``: ``int``
216219
- ``string``: ``string``
217220

218-
Doctrine will also autoconfigure any backed ``enum`` it encounters: ``type``
221+
Doctrine can also autoconfigure any backed ``enum`` it encounters: ``type``
219222
will be set to ``string`` or ``int``, depending on the enum's backing type,
220223
and ``enumType`` to the enum's |FQCN|.
221224

222-
Please note that at this time, due to backward compatibility reasons, nullable type does not imply `nullable` mapping.
225+
.. note::
226+
Nullable type does not imply ``nullable`` mapping option. You need to manually
227+
set ``nullable=true`` to have ``null`` values saved to the database.
228+
229+
Additionally Doctrine can determine ``collectionClass`` for ``ReferenceMany`` and
230+
``EmbedMany`` collections from property's type.
223231

224232
Property Mapping
225233
----------------

lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,7 @@ public function mapManyEmbedded(array $mapping): void
15631563
{
15641564
$mapping['embedded'] = true;
15651565
$mapping['type'] = self::MANY;
1566+
15661567
$this->mapField($mapping);
15671568
}
15681569

@@ -1588,6 +1589,7 @@ public function mapManyReference(array $mapping): void
15881589
{
15891590
$mapping['reference'] = true;
15901591
$mapping['type'] = self::MANY;
1592+
15911593
$this->mapField($mapping);
15921594
}
15931595

@@ -2120,7 +2122,7 @@ public function getAssociationTargetClass($assocName): ?string
21202122
throw new InvalidArgumentException("Association name expected, '" . $assocName . "' is not an association.");
21212123
}
21222124

2123-
return $this->associationMappings[$assocName]['targetDocument'];
2125+
return $this->associationMappings[$assocName]['targetDocument'] ?? null;
21242126
}
21252127

21262128
/**
@@ -2170,6 +2172,14 @@ public function mapField(array $mapping): array
21702172
$mapping['fieldName'] = $mapping['name'];
21712173
}
21722174

2175+
if ($this->isTypedProperty($mapping['fieldName'])) {
2176+
$mapping = $this->validateAndCompleteTypedFieldMapping($mapping);
2177+
2178+
if (isset($mapping['type']) && $mapping['type'] === self::MANY) {
2179+
$mapping = $this->validateAndCompleteTypedManyAssociationMapping($mapping);
2180+
}
2181+
}
2182+
21732183
if (! isset($mapping['fieldName']) || ! is_string($mapping['fieldName'])) {
21742184
throw MappingException::missingFieldName($this->name);
21752185
}
@@ -2242,14 +2252,6 @@ public function mapField(array $mapping): array
22422252
unset($this->generatorOptions['type']);
22432253
}
22442254

2245-
if ($this->isTypedProperty($mapping['fieldName'])) {
2246-
$mapping = $this->validateAndCompleteTypedFieldMapping($mapping);
2247-
2248-
if (isset($mapping['type']) && ($mapping['type'] === self::ONE || $mapping['type'] === self::MANY)) {
2249-
$mapping = $this->validateAndCompleteTypedAssociationMapping($mapping);
2250-
}
2251-
}
2252-
22532255
if (! isset($mapping['type'])) {
22542256
// Default to string
22552257
$mapping['type'] = Type::STRING;
@@ -2653,19 +2655,15 @@ private function validateAndCompleteTypedFieldMapping(array $mapping): array
26532655
*
26542656
* @return FieldMappingConfig
26552657
*/
2656-
private function validateAndCompleteTypedAssociationMapping(array $mapping): array
2658+
private function validateAndCompleteTypedManyAssociationMapping(array $mapping): array
26572659
{
26582660
$type = $this->reflClass->getProperty($mapping['fieldName'])->getType();
26592661

26602662
if (! $type instanceof ReflectionNamedType) {
26612663
return $mapping;
26622664
}
26632665

2664-
if (
2665-
! isset($mapping['collectionClass'])
2666-
&& $mapping['type'] === self::MANY
2667-
&& class_exists($type->getName())
2668-
) {
2666+
if (! isset($mapping['collectionClass']) && class_exists($type->getName())) {
26692667
$mapping['collectionClass'] = $type->getName();
26702668
}
26712669

0 commit comments

Comments
 (0)