diff --git a/.github/workflows/release-on-milestone-closed.yml b/.github/workflows/release-on-milestone-closed.yml index 951002cd7d..ecf9d0c2e0 100644 --- a/.github/workflows/release-on-milestone-closed.yml +++ b/.github/workflows/release-on-milestone-closed.yml @@ -9,6 +9,8 @@ jobs: release: name: "Git tag, release & create merge-up PR" uses: "doctrine/.github/.github/workflows/release-on-milestone-closed.yml@7.3.0" + with: + use-next-minor-as-default-branch: true secrets: GIT_AUTHOR_EMAIL: ${{ secrets.GIT_AUTHOR_EMAIL }} GIT_AUTHOR_NAME: ${{ secrets.GIT_AUTHOR_NAME }} diff --git a/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php b/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php index 44cef23070..6148832d82 100644 --- a/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php +++ b/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php @@ -218,7 +218,7 @@ public function executeInserts(array $options = []): void $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); } - $data[$versionMapping['name']] = $type->convertPHPToDatabaseValue($nextVersion); + $data[$versionMapping['name']] = $type->convertToDatabaseValue($nextVersion); } $inserts[] = $data; @@ -296,7 +296,7 @@ private function executeUpsert(object $document, array $options): void $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); } - $data['$set'][$versionMapping['name']] = $type->convertPHPToDatabaseValue($nextVersion); + $data['$set'][$versionMapping['name']] = $type->convertToDatabaseValue($nextVersion); } foreach (array_keys($criteria) as $field) { @@ -377,8 +377,8 @@ public function update(object $document, array $options = []): void $type = Type::getType($versionMapping['type']); assert($type instanceof Versionable); $nextVersion = $type->getNextVersion($currentVersion); - $update['$set'][$versionMapping['name']] = Type::convertPHPToDatabaseValue($nextVersion); - $query[$versionMapping['name']] = Type::convertPHPToDatabaseValue($currentVersion); + $update['$set'][$versionMapping['name']] = $type->convertToDatabaseValue($nextVersion); + $query[$versionMapping['name']] = $type->convertToDatabaseValue($currentVersion); } if (! empty($update)) { diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/IdTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/IdTest.php index b764a2de84..298f0c38af 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/IdTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/IdTest.php @@ -364,19 +364,22 @@ private function createIdTestClass(string $type, string $strategy): string if (! class_exists($className)) { $code = sprintf( - 'namespace Doctrine\ODM\MongoDB\Tests\Functional; - -use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; - -#[ODM\Document] -class %s -{ - #[ODM\Id(strategy: "%s", options: ["type" => "%s"])] - public $id; - - #[ODM\Field(type: "string")] - public $test = "test"; -}', + <<<'PHP' + namespace %s; + + use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; + + #[ODM\Document] + class %s + { + #[ODM\Id(strategy: "%s", options: ["type" => "%s"])] + public $id; + + #[ODM\Field(type: "string")] + public $test = "test"; + } + PHP, + __NAMESPACE__, $shortClassName, $strategy, $type, diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2789Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2789Test.php new file mode 100644 index 0000000000..bae9c72e30 --- /dev/null +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2789Test.php @@ -0,0 +1,88 @@ +dm->persist($doc); + $this->dm->flush(); + + $documents = $this->dm->getDocumentCollection(GH2789VersionedUuid::class)->find()->toArray(); + self::assertCount(1, $documents); + self::assertEquals(new Binary('1', 142), $documents[0]['version'], 'The version field should be stored using the custom type'); + + $doc->message = 'new message'; + $this->dm->persist($doc); + $this->dm->flush(); + + $documents = $this->dm->getDocumentCollection(GH2789VersionedUuid::class)->find()->toArray(); + self::assertCount(1, $documents); + self::assertEquals(new Binary('2', 142), $documents[0]['version'], 'The version field should be incremented and stored using the custom type'); + } +} + +#[ODM\Document(collection: 'gh2789_versioned_uuid')] +class GH2789VersionedUuid +{ + #[ODM\Id] + public string $id; + + #[ODM\Version] + #[ODM\Field(type: GH2789CustomType::class)] + public int $version; + + public function __construct( + #[ODM\Field(type: 'string')] + public string $message, + ) { + } +} + +/** + * Custom type that stores an integer as a MongoDB Binary subtype 142. + */ +class GH2789CustomType extends Type implements Versionable +{ + public function convertToPHPValue($value): int + { + assert($value instanceof Binary); + + return (int) $value->getData(); + } + + public function convertToDatabaseValue($value): Binary + { + assert(is_int($value)); + + return new Binary((string) $value, 142); + } + + public function getNextVersion($current): int + { + if ($current === null) { + return 1; + } + + assert(is_int($current)); + + return $current + 1; + } +} diff --git a/tests/Documents/GH2310Container.php b/tests/Documents/GH2310Container.php index 0802b5ff58..b0323a94a1 100644 --- a/tests/Documents/GH2310Container.php +++ b/tests/Documents/GH2310Container.php @@ -25,6 +25,6 @@ public function __construct(?string $id, ?GH2310Embedded $embedded) #[ODM\EmbeddedDocument] class GH2310Embedded { - #[ODM\Field(type: 'integer')] + #[ODM\Field(type: 'int')] public int $value; }