문제

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')));

도움이 되었습니까?

해결책

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')));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top