🔑 UUID as primary key
Example into docs, how to easily setup UUID as primary key.
DB column
"id" uuid PRIMARY KEY,
Assign (read-only) id in entity constructor
/**
* @property-read string $id {primary}
*/
class Foo extends Entity
{
public function __construct()
{
parent::__construct();
$this->setReadOnlyValue('id', (new \Symfony\Component\Uid\Ulid())->toRfc4122());
}
}
That should be basically it.
@mabar uuid data type was added in MariaDB 10.7. No data type support for MySQL (https://dev.mysql.com/blog-archive/mysql-8-0-uuid-support).
I've never used UUID so I'm puzzled a little. What is the issue? From ORM pov there are two things:
- mapping -> string to some type in DB -> this should be either ok or covered by doc
- setting the id programatically -> Mabar's example is quite ok
So what do you miss?
My example is for PostgreSQL. In MySQL char(36) should work just as fine, just way less performant.
I tried to use UUID with binary in MySQL and performance was almost like with native UUID, but IDs in queries became just unreadable. In Nextras ORM it can be done by setting mapping callbacks in DbalMapper, but be aware mapping of IDs is not fully supported - described in #291
So current limitation is IDs cannot be mapped to binary for database storage and cannot be mapped to a value object in php.
Hm, that would be good reason to proceed with 291.