symfony - 我可以用 Alice Fixtures 覆盖自动增量 ID 吗?

标签 symfony doctrine-orm nelmio-alice alice-fixtures

我将 Symfony2 与 Doctrine 结合使用,并使用 Alice Fixture 包来生成用于测试的装置。

在一种情况下,我需要创建一个 id 为 148 的固定装置来进行特定测试。我们所有的 id 都配置为 auto_increment,因此我当前正在创建 147 条虚拟记录,只是为了创建我需要的记录。

我希望使用这个定义来强制将 id 设置为 148:

 invoiceClassNoClass (extends invoiceClass):
    id: 148
    sortOrder: 1
    label: 'No Class'

不幸的是,这不起作用。从谷歌搜索中,我读到一条简短的评论,指出我首先需要向我的实体添加一个 setId 方法。我尝试过,但没有什么区别。实际上,如果不需要,我不想添加为 setId 方法,因为它违反了我们从不允许设置 id 的完整性规则。

也许有可以使用的反射类?也许这是 Alice 内置的,而我却不知道?

最佳答案

如果您希望学说为自动增量 ID 保存特定值,则必须在学说元数据中停用自动增量。参见

Explicitly set Id with Doctrine when using "AUTO" strategy

$this->em->persist($entity);

$metadata = $this->em->getClassMetaData(get_class($entity));
$metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());

关于symfony - 我可以用 Alice Fixtures 覆盖自动增量 ID 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36942619/

相关文章:

symfony - 在Twig中将变量转换为字符串

带有 POST 数据的 PHP Symfony 重定向

php - 我需要将 SQL native 放在查询生成器 Doctrine2 中

Symfony/Doctrine - Twig_Error_Runtime : Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string

php - 教义2 : SUM in left join

symfony - 多引用 fixture 范围@user {1..10}

mysql - Doctrine2 以错误的方式将日期类型值转换为 SQL 格式

symfony - PhpStorm 无法识别 Doctrine 的默认实体管理器

php - 使用 Alice Fixtures 为 Nested Set 实体生成 Doctrine fixture 数据

symfony - 在nelmio-alice中引用固定项目