I am using right now Symfony and Ramsey to generate mysql binary uuids and everything is working perfectly, both storage and data recovering. I want to decouple from this
library and generate my own database ids. I have created a custom class (CustomBinaryUuid) that generates Uuids from ramsey’s php uuid package (ramsey/uuid) and
converted to binary ($uuid->toBytes()). Ids are generated and stored properly.
The problem comes when recovering some data. I have three entities: user, group and comment. Groups have owner users and joined users (through pivot table). Comments belong to users and groups.
When I load the data from the User entity (their owned groups, joined groups and comments) with Ramsey it is able to recover ALL the information properly. But when trying to do the same using CustomBinaryUuid
and getting the data from the PersistentCollections with this code:
$ownedGroups = $entityItem->getOwnedGroups()->getValues();
$joinedGroups = $entityItem->getJoinedGroups()->getValues();
$comments = $entityItem->getComments()->getValues();
it doesn’t fetch “comments”. If I execute the following code just moving “comments” to the first position, only comments are recovered, ownedGroups and joinedGroups are empty.
$comments = $entityItem->getComments()->getValues();
$ownedGroups = $entityItem->getOwnedGroups()->getValues();
$joinedGroups = $entityItem->getJoinedGroups()->getValues();
With Ramsey, on both cases, all the data from all entities is recovered properly.
The only change I do from Ramsey to CustomBinaryUuid is on the Entities orm xml:
<!-- Ramsey -->
<id name="id" type="uuid_binary" column="id">
<generator strategy="CUSTOM"/>
<custom-id-generator class="RamseyUuidDoctrineUuidV7Generator"/>
</id>
<!-- CustomBinaryUuid -->
<id name="id" type="binary" column="id" />
I have noticed that if I change just one entity id to CustomBinaryUuid, only this entity doesn’t recover all the information, the other entities using Ramsey get everything.
This is the CustomBinaryUuid class:
class CustomBinaryUuid
{
public function __construct(private $uuid) {}
public static function random(): self
{
$factory = new UuidFactory;
$factory->setRandomGenerator(new CombGenerator(
$factory->getRandomGenerator(),
$factory->getNumberConverter()
));
$factory->setCodec(new GuidStringCodec(
$factory->getUuidBuilder()
));
return new self($factory->uuid7());
}
public static function fromString(string $uuid): self
{
$factory = new UuidFactory;
$uuidElement = $factory->fromString($uuid);
return new self($uuidElement);
}
public function toBinary()
{
return $this->uuid->getBytes();
}
public function toString(): string
{
return $this->uuid->toString();
}
}
I have been digging into Ramsey documentation and code with no luck of finding what is special on it.
What am I missing?
Thanks you.