spacecodeur
spacecodeur

Reputation: 2406

Symfony : share a non-entity object between fixtures

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 !

Upvotes: 0

Views: 201

Answers (1)

simon.ro
simon.ro

Reputation: 3312

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
{
}

Upvotes: 1

Related Questions