英文:
Symfony : share a non-entity object between fixtures
问题
在Symfony 6中,我们可以使用方法addReference
和getReference
轻松在多个夹具类之间共享实体对象。但是这些方法只处理由实体管理器管理的实体类。
如果我在第一个夹具中创建一个简单的值(整数、字符串),并在./bin/console d:fixture:load
脚本期间如何将该变量传递给后续的夹具?
感谢任何帮助!
英文:
On Symfony 6, we can easly share an entty object through several fixtures class with the methods addReference
and getReference
. But theses methods handle only entities class managed by the entityManager.
If I create a simple value in a variable (an integer, a string) initialized during the first fixture, how can I pass the variable to the next fixtures called during the ./bin/console d:fixture:load
script ?
Thanks for any help !
答案1
得分: 1
addReference()
和 getReference
可以被你所有的 fixture 类访问,因为它们是在一个通用的基类 Doctrine\Common\DataFixtures\AbstractFixture
中实现的。
如果你想分享更多内容,只需使用相同的思路:
引入你自己的父类 到继承结构中,然后将你想要分享的一切存储在该父类上:
abstract class MyFixturesParent extends AbstractFixture
{
protected int $mySharedInt = 1;
}
然后将你的 Fixtures 实现为该父类的子类:
class MyFixtures extends MyFixturesParent
{
}
英文:
addReference()
and getReference
are accessible by all your fixture classes, because they are implemented in a common base class Doctrine\Common\DataFixtures\AbstractFixture
.
If you want to share more than that, just make use of the same idea:
Introduce your own parent class into the hierarchy and store everything you want to share on that parent:
abstract class MyFixturesParent extends AbstractFixture
{
protected int $mySharedInt = 1;
}
and implement your Fixtures as children of that parent
class MyFixtures extends MyFixturesParent
{
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论