|
12 | 12 | use Doctrine\ODM\MongoDB\Event\OnClassMetadataNotFoundEventArgs;
|
13 | 13 | use Doctrine\ODM\MongoDB\Events;
|
14 | 14 | use Doctrine\ODM\MongoDB\Id\AlnumGenerator;
|
15 |
| -use Doctrine\ODM\MongoDB\Id\AutoGenerator; |
16 | 15 | use Doctrine\ODM\MongoDB\Id\IdGenerator;
|
17 | 16 | use Doctrine\ODM\MongoDB\Id\IncrementGenerator;
|
| 17 | +use Doctrine\ODM\MongoDB\Id\ObjectIdGenerator; |
| 18 | +use Doctrine\ODM\MongoDB\Id\SymfonyUuidGenerator; |
18 | 19 | use Doctrine\ODM\MongoDB\Id\UuidGenerator;
|
19 | 20 | use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
|
20 | 21 | use Doctrine\Persistence\Mapping\ClassMetadata as ClassMetadataInterface;
|
21 | 22 | use Doctrine\Persistence\Mapping\Driver\MappingDriver;
|
22 | 23 | use Doctrine\Persistence\Mapping\ReflectionService;
|
23 | 24 | use ReflectionException;
|
| 25 | +use ReflectionNamedType; |
24 | 26 |
|
25 | 27 | use function assert;
|
26 | 28 | use function get_class_methods;
|
@@ -186,7 +188,7 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS
|
186 | 188 | if ($parent->idGenerator) {
|
187 | 189 | $class->setIdGenerator($parent->idGenerator);
|
188 | 190 | }
|
189 |
| - } else { |
| 191 | + } elseif ($class->identifier) { |
190 | 192 | $this->completeIdGeneratorMapping($class);
|
191 | 193 | }
|
192 | 194 |
|
@@ -230,12 +232,36 @@ protected function newClassMetadataInstance($className): ClassMetadata
|
230 | 232 | return new ClassMetadata($className);
|
231 | 233 | }
|
232 | 234 |
|
| 235 | + private function generateAutoIdGenerator(ClassMetadata $class): void |
| 236 | + { |
| 237 | + $identifierMapping = $class->getIdentifierMapping(); |
| 238 | + switch ($identifierMapping['type']) { |
| 239 | + case 'id': |
| 240 | + case 'objectId': |
| 241 | + $class->setIdGenerator(new ObjectIdGenerator()); |
| 242 | + break; |
| 243 | + case 'uuid': |
| 244 | + $reflectionProperty = $class->getReflectionProperty($identifierMapping['fieldName']); |
| 245 | + if (! $reflectionProperty->getType() instanceof ReflectionNamedType) { |
| 246 | + throw MappingException::autoIdGeneratorNeedsType($class->name, $identifierMapping['fieldName']); |
| 247 | + } |
| 248 | + |
| 249 | + $class->setIdGenerator(new SymfonyUuidGenerator($reflectionProperty->getType()->getName())); |
| 250 | + break; |
| 251 | + default: |
| 252 | + throw MappingException::unsupportedTypeForAutoGenerator( |
| 253 | + $class->name, |
| 254 | + $identifierMapping['type'], |
| 255 | + ); |
| 256 | + } |
| 257 | + } |
| 258 | + |
233 | 259 | private function completeIdGeneratorMapping(ClassMetadata $class): void
|
234 | 260 | {
|
235 | 261 | $idGenOptions = $class->generatorOptions;
|
236 | 262 | switch ($class->generatorType) {
|
237 | 263 | case ClassMetadata::GENERATOR_TYPE_AUTO:
|
238 |
| - $class->setIdGenerator(new AutoGenerator()); |
| 264 | + $this->generateAutoIdGenerator($class); |
239 | 265 | break;
|
240 | 266 | case ClassMetadata::GENERATOR_TYPE_INCREMENT:
|
241 | 267 | $incrementGenerator = new IncrementGenerator();
|
|
0 commit comments