Question

I understand how to setup load fixtures for a OneToMany with the following:

$this->addReference('category-1', $category1);

and using it with the following:

$blog1->setCategory($manager->merge($this->getReference('category-1')));

But how would you do this for a ManyToMany relationship, if the blog has 2 or more categories?

Would you just double up the lines in the fixture so you have 2 or more ->setCategory() fields

$blog1->setCategory($manager->merge($this->getReference('category-1')));

$blog1->setCategory($manager->merge($this->getReference('category-2')));

or use

$blog1->setCategory($manager->merge($this->getReference('category-1', 'category-2')));

Was it helpful?

Solution

If you have a look at the Doctrine\Common\DataFixtures\AbstractFixture class and view the ->getReference() function it will show that variable passed in for the function is for a string.

This means that the first example, calling getReference twice is the way it will work.

$blog1->setCategory($manager->merge($this->getReference('category-1')));
$blog1->setCategory($manager->merge($this->getReference('category-2')));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top