我想以RFC 4122格式(f.e )为新的实体生成I。( c9bb9b5a-2950-4f02-a9df-3925a0b62513)
https://symfony.com/doc/5.3/components/uid.html#generating-ulids https://symfony.com/bundles/DoctrineBundle/current/custom-id-generators.html
我首先尝试了我自己的IdGenerator类:
<?php
namespace App\Service;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Id\AbstractIdGenerator;
use Symfony\Component\Uid\Uuid;
class IdGenerator extends AbstractIdGenerator
{
public function generate(EntityManager $em, $entity)
{
$uuid = Uuid::v4();
return $uuid;
}
}实体:
/**
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="App\Service\IdGenerator")
*/
private $id;我用XDebug调试了它。它进入了正确的类和函数,创建并返回了正确的UUID,但是当我试图加载我的夹具时,我仍然会得到相同的错误:
An exception occurred while executing 'INSERT INTO step (id, text, periodically, necessary, activated, created_at, modified_at, created_b
y, modified_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["\x2b\x06\x41\x56\x73\xb0\x4f\xd2\x80\xfa\xa2\x87\xd1\xed\x31\x5f", "ste
p1", 0, 0, 1, "2022-02-12 23:35:58", "2022-02-12 23:35:58", "housemeister-system", "housemeister-system"]:
SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xB0O\xD2\x80\xFA\xA2...' for column 'id' at row 1 不知何故,它仍然试图将其插入为二进制文件。
我的第二次尝试是在实体的构造函数中手动生成它:
public function __construct()
{
$uuid = Uuid::v4()->__toString();
$this->id = $uuid;
}同样的结果,同样的错误信息。有人想到办法了吗?
编辑:最近更新的理论在我的Symfony 5.3项目。这一更新似乎将id类型从char(36)改为二进制(16)。也许上下文信息有帮助。
发布于 2022-11-10 14:50:46
我也有同样的问题,我用:
composer require ramsey/uuid-doctrinehttps://stackoverflow.com/questions/71096793
复制相似问题