ramsey/uuid-doctrine

Type missmatch in generator with static PHP Mapping (LazyUuidFromString to property Uuid)

xuedi opened this issue · 3 comments

xuedi commented

Hello,
i got a type missmatch on persist:
Cannot assign Ramsey\Uuid\Lazy\LazyUuidFromString to property SharedBookshelf\Entities\User::$id of type Ramsey\Uuid\Uuid
versions:

doctrine/orm: 2.8.1
ramsey/uuid-doctrine: 1.6.0

The entity code:

<?php declare(strict_types=1);

namespace SharedBookshelf\Entities;

use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
use Ramsey\Uuid\Doctrine\UuidGenerator;
use Ramsey\Uuid\Uuid;
use SharedBookshelf\Repositories\UserRepository;

class User implements Entity
{
    private Uuid $id;
    private string $username = '';
    private string $password = '';

    public static function loadMetadata(ClassMetadata $metadata): void
    {
        $builder = new ClassMetadataBuilder($metadata);
        $builder->createField('id', 'uuid')
            ->makePrimaryKey()
            ->generatedValue('CUSTOM')
            ->setCustomIdGenerator(UuidGenerator::class)
            ->build();
        $builder->setCustomRepositoryClass(UserRepository::class);
        $builder->addField('username', 'string');
        $builder->addField('password', 'string');
    }

    public function getId(): Uuid
    {
        return $this->id;
    }

    public function getUsername(): string
    {
        return $this->username;
    }

    public function setUsername(string $username): void
    {
        $this->username = $username;
    }

    public function getPassword(): string
    {
        return $this->password;
    }

    public function setPassword(string $password): void
    {
        $this->password = $password;
    }

}

i am not 100% sure if this is due to the fact that i use the static php meta function, i took your annotation example as the basis in combination with: https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/reference/php-mapping.html#php-mapping

The detailed error with trace (project: https://github.com/xuedi/sharedBookshelf):

Details
Type: TypeError
Code: 0
Message: Cannot assign Ramsey\Uuid\Lazy\LazyUuidFromString to property SharedBookshelf\Entities\User::$id of type Ramsey\Uuid\Uuid
File: /home/xuedi/projects/sharedBookshelf/vendor/doctrine/persistence/lib/Doctrine/Persistence/Reflection/TypedNoDefaultReflectionProperty.php
Line: 46
Trace

#0 /home/xuedi/projects/sharedBookshelf/vendor/doctrine/persistence/lib/Doctrine/Persistence/Reflection/TypedNoDefaultReflectionProperty.php(46): ReflectionProperty->setValue()
#1 /home/xuedi/projects/sharedBookshelf/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php(762): Doctrine\Persistence\Reflection\TypedNoDefaultReflectionProperty->setValue()
#2 /home/xuedi/projects/sharedBookshelf/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(964): Doctrine\ORM\Mapping\ClassMetadataInfo->setIdentifierValues()
#3 /home/xuedi/projects/sharedBookshelf/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(1790): Doctrine\ORM\UnitOfWork->persistNew()
#4 /home/xuedi/projects/sharedBookshelf/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(1746): Doctrine\ORM\UnitOfWork->doPersist()
#5 /home/xuedi/projects/sharedBookshelf/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(614): Doctrine\ORM\UnitOfWork->persist()
#6 /home/xuedi/projects/sharedBookshelf/src/Repositories/UserRepository.php(19): Doctrine\ORM\EntityManager->persist()
#7 /home/xuedi/projects/sharedBookshelf/src/Controller/SignupController.php(65): SharedBookshelf\Repositories\UserRepository->save()
#8 /home/xuedi/projects/sharedBookshelf/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(43): SharedBookshelf\Controller\SignupController->index()
#9 /home/xuedi/projects/sharedBookshelf/vendor/slim/slim/Slim/Routing/Route.php(384): Slim\Handlers\Strategies\RequestResponse->__invoke()
#10 /home/xuedi/projects/sharedBookshelf/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\Routing\Route->handle()
#11 /home/xuedi/projects/sharedBookshelf/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\MiddlewareDispatcher->handle()
#12 /home/xuedi/projects/sharedBookshelf/vendor/slim/slim/Slim/Routing/Route.php(341): Slim\MiddlewareDispatcher->handle()
#13 /home/xuedi/projects/sharedBookshelf/vendor/slim/slim/Slim/Routing/RouteRunner.php(84): Slim\Routing\Route->run()
#14 /home/xuedi/projects/sharedBookshelf/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(107): Slim\Routing\RouteRunner->handle()
#15 /home/xuedi/projects/sharedBookshelf/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process()
#16 /home/xuedi/projects/sharedBookshelf/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Psr\Http\Server\RequestHandlerInterface@anonymous->handle()
#17 /home/xuedi/projects/sharedBookshelf/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher->handle()
#18 /home/xuedi/projects/sharedBookshelf/vendor/slim/slim/Slim/App.php(199): Slim\App->handle()
#19 /home/xuedi/projects/sharedBookshelf/src/Framework.php(58): Slim\App->run()
#20 /home/xuedi/projects/sharedBookshelf/src/Factory.php(63): SharedBookshelf\Framework->run()
#21 /home/xuedi/projects/sharedBookshelf/public/index.php(17): SharedBookshelf\Factory->run()
#22 {main}

This seems to be because of ramsey/uuid#327 and the addition of LazyUuidFromString in 4.1. I'm having a similar issue with the version 4.1 of ramsey/uuid

You are supposed to declare the type to be UuidInterface, not Uuid, which is only one possible implementation of a UuidInterface.

This is a bug in your type declaration, not in this library.

This is explained in ramsey/uuid#327 (comment)

As @Ocramius said, please use UuidInterface instead of Uuid. Thanks!