kc_*_*rob 7 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'
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用.从谷歌搜索我读了一个简短的评论,说我首先需要添加一个setId方法到我的实体.我试过了,但没有什么区别.实际上,如果我不需要,我不想添加为setId方法,因为它违反了我们永远不允许设置id的完整性规则.
也许有可以使用的反射类?也许这是爱丽丝的内置而我不知道呢?
小智 8
如果您希望doctrine为自动增量Id保存特定值,则必须在doctrine元数据中停用自动增量.看到
$this->em->persist($entity);
$metadata = $this->em->getClassMetaData(get_class($entity));
$metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
Run Code Online (Sandbox Code Playgroud)
更新:经过对该主题的进一步研究,我意识到固定装置中的硬编码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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在测试的setUp()方法中,我有
$this->allowFixedIdsFor([
MyEntity::class,
]);
$this->loadFixtureFiles([
'./tests/DataFixtures/ORM/my_entities.yml',
]);
$this->recoverIdGenerators();
Run Code Online (Sandbox Code Playgroud)
看起来你不需要在你的实体中使用setId()方法.
我也遇到过这个问题,因为我希望每次运行时都有一些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);
}
Run Code Online (Sandbox Code Playgroud)
$metadata
为您想要拥有自定义ID的所有类执行-stuff(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);
}
Run Code Online (Sandbox Code Playgroud)
2)我创建了一个模板来生成id
类似的学说.
id (template):
id (unique): '<uuid()>'
Run Code Online (Sandbox Code Playgroud)
3)在我的自定义实体夹具中,现在我可以执行以下操作:
app_custom (extends id):
id: 'whatever-i-want'
title: 'My custom entity'
Run Code Online (Sandbox Code Playgroud)
4)如果我不希望使用自定义的ID,我只是不覆盖的id
领域:
app_another (extends id):
title: 'Just another app with an id I do not care about'
Run Code Online (Sandbox Code Playgroud)
证明:
归档时间: |
|
查看次数: |
3251 次 |
最近记录: |