Skip to content

[UId/Doctrine] Entity repository error "ConversionException" #38929

@94noni

Description

@94noni

Symfony version(s) affected: 5.2.0-beta3

Description

Entity repository failed to retrieve an entity with an Uid property with method findOneBy

Doctrine\DBAL\Types\ConversionException:
Could not convert PHP value 'e2e64173-174b-4109-a3ce-971a1e9d3b56' of type 'string' to type 'uuid'.
Expected one of the following types: null, Symfony\Component\Uid\AbstractUid

How to reproduce

See code after

Possible Solution

If I replace the doctrine findOneByUuid with a personal repo method findMyHumidorWithUuid it works.

Additional context

I am using Messenger and pass the object uuid to the async logic, then inside the handler I am trying to retrieve the original object, and the error occurs.

Some code:

use Symfony\Component\Uid\Uuid;
[...]
/**
 * @ORM\Entity(repositoryClass="App\Repository\HumidorRepository")
 */
class Humidor
{
[...]
    /**
     * @ORM\Column(type="uuid", unique=true)
     */
    private $uuid;
[...]
    public function __construct()
    {
        $this->uuid = Uuid::v4();
[...]
    }
}

Failure code:

// in code
// $uuid is 'e2e64173-174b-4109-a3ce-971a1e9d3b56'
$humidor = $humidorRepository->findOneByUuid($uuid);
See stack trace (inside a controller reproducer)
Doctrine\DBAL\Types\ConversionException:
Could not convert PHP value 'e2e64173-174b-4109-a3ce-971a1e9d3b56' of type 'string' to type 'uuid'. Expected one of the following types: null, Symfony\Component\Uid\AbstractUid

  at vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/ConversionException.php:79
  at Doctrine\DBAL\Types\ConversionException::conversionFailedInvalidType('e2e64173-174b-4109-a3ce-971a1e9d3b56', 'uuid', array('null', 'Symfony\\Component\\Uid\\AbstractUid'))
     (vendor/symfony/doctrine-bridge/Types/AbstractUidType.php:68)
  at Symfony\Bridge\Doctrine\Types\AbstractUidType->convertToDatabaseValue('e2e64173-174b-4109-a3ce-971a1e9d3b56', object(MySQL57Platform))
     (vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:1997)
  at Doctrine\DBAL\Connection->getBindingInfo('e2e64173-174b-4109-a3ce-971a1e9d3b56', object(UuidType))
     (vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:2030)
  at Doctrine\DBAL\Connection->resolveParams(array('e2e64173-174b-4109-a3ce-971a1e9d3b56'), array('uuid'))
     (vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:2121)
  at Doctrine\DBAL\Connection->handleExceptionDuringQuery(object(ConversionException), 'SELECT t0.id AS id_1, t0.uuid AS uuid_2, t0.name AS name_3, t0.brand AS brand_4, t0.private AS private_5, t0.temperature AS temperature_6, t0.humidity AS humidity_7, t0.humidification_system AS humidification_system_8, t0.user_id AS user_id_9 FROM humidor t0 WHERE t0.uuid = ? LIMIT 1', array('e2e64173-174b-4109-a3ce-971a1e9d3b56'), array('uuid'))
     (vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:1264)
  at Doctrine\DBAL\Connection->executeQuery('SELECT t0.id AS id_1, t0.uuid AS uuid_2, t0.name AS name_3, t0.brand AS brand_4, t0.private AS private_5, t0.temperature AS temperature_6, t0.humidity AS humidity_7, t0.humidification_system AS humidification_system_8, t0.user_id AS user_id_9 FROM humidor t0 WHERE t0.uuid = ? LIMIT 1', array('e2e64173-174b-4109-a3ce-971a1e9d3b56'), array('uuid'))
     (vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:750)
  at Doctrine\ORM\Persisters\Entity\BasicEntityPersister->load(array('uuid' => 'e2e64173-174b-4109-a3ce-971a1e9d3b56'), null, null, array(), null, 1, null)
     (vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:193)
  at Doctrine\ORM\EntityRepository->findOneBy(array('uuid' => 'e2e64173-174b-4109-a3ce-971a1e9d3b56'))
     (vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:311)
  at Doctrine\ORM\EntityRepository->resolveMagicCall('findOneBy', 'Uuid', array('e2e64173-174b-4109-a3ce-971a1e9d3b56'))
     (vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:228)
  at Doctrine\ORM\EntityRepository->__call('findOneByUuid', array('e2e64173-174b-4109-a3ce-971a1e9d3b56'))
     (src/Controller/Account/CigarController.php:73)
  at App\Controller\Account\CigarController->add(object(Request), object(CigarManager), object(HumidorRepository))
     (vendor/symfony/http-kernel/HttpKernel.php:157)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:79)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:195)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:22)    

Working code:

// in the repo
    public function findHumidorByUuid(string $uuid): ?Humidor
    {
        $qb = $this->createQueryBuilder('h')
            ->andWhere('h.uuid = :uuid')
            ->setParameter('uuid', $uuid);

        return $qb->getQuery()->getOneOrNullResult();
    }

// in code
// $uuid is 'e2e64173-174b-4109-a3ce-971a1e9d3b56'
$humidor = $humidorRepository->findHumidorByUuid($uuid);

It is linked to Doctrine of course, but as the UidType logic is inside Symfony I opened the issue here
Thank you

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions