我正在尝试使用 yml 文件将 Id 属性映射为 Symfony\Component\Ulid。
这是我的 yml 文件:
App\Entity\User:
type: entity
repositoryClass: App\Entity\UserRepository
table: user
id:
id:
type: ulid
unique: true
generator:
strategy: CUSTOM
class: Symfony\Component\Uid\Ulid
当我运行命令生成“迁移”时,出现以下错误:“无法实例化自定义生成器,没有定义类”
如何使用 yml 文件将 Id 属性映射到 Ulid 类型?
下面使用 PHP 8 属性的映射有效:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Ulid;
class User
{
#[ORM\Id]
#[ORM\Column(type: 'ulid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: 'doctrine.ulid_generator')]
private $id;
public function getId(): ?Ulid
{
return $this->id;
}
}
但我需要使用 yml 的映射。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我解决的方法如下:
App\Entity\User: type: entity repositoryClass: App\Repository\UserRepository table: "`user`" id: id: type: ulid unique: true generator: strategy: CUSTOM customIdGenerator: class: doctrine.ulid_generator