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

时间:2016-04-29 15:47:46

标签: 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的完整性规则。

也许有可以使用的反射类?也许这是内置于爱丽丝,我不知道它?

3 个答案:

答案 0 :(得分:7)

如果您希望doctrine为自动增量Id保存特定值,则必须停用doctrine元数据中的自动增量。见

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());

答案 1 :(得分:6)

<强>更新: 经过对该主题的进一步研究后,我意识到固定装置中的硬编码ID会带来其自身的问题,通常不是一个好主意。相反,当您需要检查特定记录时,最好通过yml文件中的引用来获取fixture。

由于问题是关于Alice fixtures捆绑包,我发现以下内容对我有用:

protected $idGeneratorTypes = [];

protected function allowFixedIdsFor(array $entityClasses)
{
    $em = $this->getContainer()->get('doctrine')->getManager();
    foreach ($entityClasses as $entityClass) {
        $metadata = $em->getClassMetadata($entityClass);
        $this->idGeneratorTypes[$entityClass] = $metadata->generatorType;
        $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
    }
}

protected function recoverIdGenerators()
{
    $em = $this->getContainer()->get('doctrine')->getManager();
    foreach ($this->idGeneratorTypes as $entityClass => $idGeneratorType) {
        $metadata = $em->getClassMetadata($entityClass);
        $metadata->setIdGeneratorType($idGeneratorType);
    }
}

然后在测试的 setUp()方法中,我有

    $this->allowFixedIdsFor([
        MyEntity::class,
    ]);


    $this->loadFixtureFiles([
        './tests/DataFixtures/ORM/my_entities.yml',
    ]);

    $this->recoverIdGenerators();

看起来您不需要在您的实体中使用 setId()方法。

答案 2 :(得分:5)

我也遇到过这个问题,因为我希望每次运行时都有一些ID,因为我想使用UUID策略在我的调试应用中引用它们。

我做了什么:

1)写了一个包裹hautelook:fixtures:load的命令:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $container     = $this->getContainer();
    $entityManager = $container->get('doctrine.orm.default_entity_manager');

    $metadata = $entityManager->getClassMetaData(App::class);
    $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
    $metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());

    $application = new Application($kernel);
    $application->setAutoExit(false);

    $this->loadFixtures($application, $output);
}

为您想要拥有自定义ID($metadata)的所有课程执行App::class - 。

protected function loadFixtures(Application $application, OutputInterface $output)
{
    $loadFixturesInput = new ArrayInput(array(
        'command' => 'hautelook:fixtures:load',
        '--no-interaction' => 'true',
        '--verbose' => '3'
    ));

    $application->run($loadFixturesInput, $output);
}

2)我创建了一个模板来生成像id这样的学说。

id (template):
    id (unique):   '<uuid()>'

3)在我的自定义实体夹具中,现在我可以执行以下操作:

app_custom (extends id):
    id:    'whatever-i-want'
    title: 'My custom entity'

4)如果我想要使用自定义ID,我就不会覆盖id字段:

app_another (extends id):
    title: 'Just another app with an id I do not care about'

证明:

enter image description here

相关问题