Skip to content

[DoctrineBridge] Ulid and Uuid as Doctrine Types #37678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

5.2.0
-----

* added support for symfony/uid as `UlidType`, `UuidType`, `UlidBinaryType` and `UuidBinaryType` as Doctrine types
* added `UlidGenerator`, `UUidV1Generator`, `UuidV4Generator` and `UuidV6Generator`

5.0.0
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass;

use Symfony\Bridge\Doctrine\Types\UlidType;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Uid\AbstractUid;

final class RegisterUidTypePass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!class_exists(AbstractUid::class)) {
return;
}

$typeDefinition = $container->getParameter('doctrine.dbal.connection_factory.types');

if (!isset($typeDefinition['uuid'])) {
$typeDefinition['uuid'] = ['class' => UuidType::class];
}

if (!isset($typeDefinition['ulid'])) {
$typeDefinition['ulid'] = ['class' => UlidType::class];
}

$container->setParameter('doctrine.dbal.connection_factory.types', $typeDefinition);
}
}
24 changes: 24 additions & 0 deletions src/Symfony/Bridge/Doctrine/Id/UlidGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Types;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The namespace does not match the folder path. BTW, why not call it "Generator"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you send a PR please to fix the namespace?
and change the namespace to Uid meanwhile

why not call it "Generator"?

not sure what you mean here sorry...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not call it "Generator"?

got it!
yes, let's name it Generator
I'm looking forward to your PR :)
If you can add tests, that'd be awesome.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IdGenerator actually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it was just not very clear to have the namespace "Id" but "Uid" looks better to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The namespace does not match the folder path. BTW, why not call it "Generator"?

Good catch.
I just haven't spent enough time with implementing those Generators.

Thx.


use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Id\AbstractIdGenerator;
use Symfony\Component\Uid\Ulid;

final class UlidGenerator extends AbstractIdGenerator
{
public function generate(EntityManager $em, $entity): Ulid
{
return new Ulid();
}
}
24 changes: 24 additions & 0 deletions src/Symfony/Bridge/Doctrine/Id/UuidV1Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Types;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Id\AbstractIdGenerator;
use Symfony\Component\Uid\UuidV1;

final class UuidV1Generator extends AbstractIdGenerator
{
public function generate(EntityManager $em, $entity): UuidV1
{
return new UuidV1();
}
}
24 changes: 24 additions & 0 deletions src/Symfony/Bridge/Doctrine/Id/UuidV4Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Types;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Id\AbstractIdGenerator;
use Symfony\Component\Uid\UuidV4;

final class UuidV4Generator extends AbstractIdGenerator
{
public function generate(EntityManager $em, $entity): UuidV4
{
return new UuidV4();
}
}
24 changes: 24 additions & 0 deletions src/Symfony/Bridge/Doctrine/Id/UuidV6Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Types;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Id\AbstractIdGenerator;
use Symfony\Component\Uid\UuidV6;

final class UuidV6Generator extends AbstractIdGenerator
{
public function generate(EntityManager $em, $entity): UuidV6
{
return new UuidV6();
}
}
119 changes: 119 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Types/UlidBinaryTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Types\UlidBinaryType;
use Symfony\Component\Uid\Ulid;

class UlidBinaryTypeTest extends TestCase
{
private const DUMMY_ULID = '01EEDQEK6ZAZE93J8KG5B4MBJC';

private $platform;

/** @var UlidBinaryType */
private $type;

public static function setUpBeforeClass(): void
{
Type::addType('ulid_binary', UlidBinaryType::class);
}

protected function setUp(): void
{
$this->platform = $this->createMock(AbstractPlatform::class);
$this->platform
->method('getBinaryTypeDeclarationSQL')
->willReturn('DUMMYBINARY(16)');

$this->type = Type::getType('ulid_binary');
}

public function testUlidConvertsToDatabaseValue()
{
$uuid = Ulid::fromString(self::DUMMY_ULID);

$expected = $uuid->toBinary();
$actual = $this->type->convertToDatabaseValue($uuid, $this->platform);

$this->assertEquals($expected, $actual);
}

public function testStringUlidConvertsToDatabaseValue()
{
$expected = Ulid::fromString(self::DUMMY_ULID)->toBinary();
$actual = $this->type->convertToDatabaseValue(self::DUMMY_ULID, $this->platform);

$this->assertEquals($expected, $actual);
}

public function testInvalidUlidConversionForDatabaseValue()
{
$this->expectException(ConversionException::class);

$this->type->convertToDatabaseValue('abcdefg', $this->platform);
}

public function testNotSupportedTypeConversionForDatabaseValue()
{
$this->assertNull($this->type->convertToDatabaseValue(new \stdClass(), $this->platform));
}

public function testNullConversionForDatabaseValue()
{
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}

public function testUlidConvertsToPHPValue()
{
$uuid = $this->type->convertToPHPValue(self::DUMMY_ULID, $this->platform);

$this->assertEquals(self::DUMMY_ULID, $uuid->__toString());
}

public function testInvalidUlidConversionForPHPValue()
{
$this->expectException(ConversionException::class);

$this->type->convertToPHPValue('abcdefg', $this->platform);
}

public function testNullConversionForPHPValue()
{
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
}

public function testReturnValueIfUlidForPHPValue()
{
$uuid = new Ulid();
$this->assertSame($uuid, $this->type->convertToPHPValue($uuid, $this->platform));
}

public function testGetName()
{
$this->assertEquals('ulid_binary', $this->type->getName());
}

public function testGetGuidTypeDeclarationSQL()
{
$this->assertEquals('DUMMYBINARY(16)', $this->type->getSqlDeclaration(['length' => 36], $this->platform));
}

public function testRequiresSQLCommentHint()
{
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
}
}
Loading