Search code examples
phpdatabasesymfonydoctrineuuid

Doctrine findBy array of UUIDs


I'm having a problem retrieving users by UUIDs using Doctrine in my Symfony application, and after a few hours of struggling without any real leads, I decided to ask the question here in case I missed something obvious.

Problem:
I have a Symfony application where users are identified by UUIDs.I'm trying to retrieve users from a list of UUIDs using Doctrine's findBy method, but I systematically get an empty array even though the UUIDs exist in the database.

Here my $id field:

trait UuidTrait
{
    #[ORM\Id]
    #[ORM\Column(type: UuidType::NAME, unique: true)]
    #[ORM\GeneratedValue(strategy: 'CUSTOM')]
    #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
    private ?Uuid $id = null;

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

My Code to retrieve users:

$ids = ["018dd095-4c15-78b4-8f45-6c0bdc78368e","018dd226-5561-7b24-8677-64fa33a0267b"];
$users = $userRepository->findBy(["id" => $ids]); // -> empty array

I tried to convert my strings to UUIDs with:

$ids = array_map(fn (string $id) => Uuid::fromString($id), $ids);

But it still didn't work...

I checked my ids:

$users = $userRepository->findBy(["id" => $ids[0]]);

And $users contained 1 user so my ids were good.

The worst thing is that there are no errors thrown so I don't know where the problem is. I've seen several people use an array as a criteria parameter (here) so I assumed it was working.

Thanks in advance to anyone who can help me.

Geod

EDIT: Is saw this in the doctrine documentation and my code looks correct


Solution

  • I had same problem.

    Looks like converting it to binary helps (I found it here).

    This should do the trick:

    $ids = array_map(fn (string $id): string => Uuid::fromString($id)->toBinary(), $ids);
    $users = $userRepository->findBy(["id" => $ids]);