Symfony:在夹具之间共享非实体对象。

huangapple go评论48阅读模式
英文:

Symfony : share a non-entity object between fixtures

问题

在Symfony 6中,我们可以使用方法addReferencegetReference轻松在多个夹具类之间共享实体对象。但是这些方法只处理由实体管理器管理的实体类。

如果我在第一个夹具中创建一个简单的值(整数、字符串),并在./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
{
}

huangapple
  • 本文由 发表于 2023年2月9日 02:19:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定