8000 add integer identity trait (#382) · msgphp/msgphp@28f9646 · GitHub
[go: up one dir, main page]

Skip to content

Commit 28f9646

Browse files
authored
add integer identity trait (#382)
1 parent ee66516 commit 28f9646

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/Domain/Infrastructure/Doctrine/DomainObjectMappings.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MsgPhp\Domain\Infrastructure\Doctrine;
66

7+
use Doctrine\ORM\Mapping\ClassMetadata;
78
use MsgPhp\Domain\Model;
89

910
/**
@@ -15,6 +16,13 @@ final class DomainObjectMappings implements ObjectMappingProvider
1516
{
1617
public static function provideObjectMappings(MappingConfig $config): iterable
1718
{
19+
yield Model\IntIdentity::class => [
20+
'id' => [
21+
'id' => true,
22+
'id_generator' => ClassMetadata::GENERATOR_TYPE_IDENTITY,
23+
'type' => 'integer',
24+
],
25+
];
1826
yield Model\CanBeConfirmed::class => [
1927
'confirmationToken' => [
2028
'type' => 'string',

src/Domain/Infrastructure/Doctrine/Event/ObjectMappingListener.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace MsgPhp\Domain\Infrastructure\Doctrine\Event;
66

77
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
8+
use Doctrine\ORM\Id\AbstractIdGenerator;
89
use Doctrine\ORM\Mapping\ClassMetadataFactory;
910
use Doctrine\ORM\Mapping\ClassMetadataInfo;
1011
use MsgPhp\Domain\Infrastructure\Doctrine\MappingConfig;
@@ -89,6 +90,15 @@ private function processFieldMapping(ClassMetadataInfo $metadata, array $mapping
8990
continue;
9091
}
9192

93+
if (null !== $idGenerator = $info['id_generator'] ?? null) {
94+
if ($idGenerator instanceof AbstractIdGenerator) {
95+
$metadata->setIdGenerator($idGenerator);
96+
} else {
97+
$metadata->setIdGeneratorType($idGenerator);
98+
}
99+
unset($info['id_generator']);
100+
}
101+
92102
$info = ['fieldName' => $field] + $info;
93103

94104
if (isset($info['type']) && method_exists($metadata, $method = 'map'.ucfirst($info['type']))) {

src/Domain/Model/IntIdentity.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\Domain\Model;
6+
7+
/**
8+
* @author Roland Franssen <franssen.roland@gmail.com>
9+
*/
10+
trait IntIdentity
11+
{
12+
/** @var null|int */
13+
private $id;
14+
}

0 commit comments

Comments
 (0)
0