Doctrine灯具不会按给定顺序加载,而是按字母顺序加载

时间:2015-06-06 22:44:29

标签: symfony doctrine-orm

据我所知,灯具将按照给定的顺序加载,基于每个灯具文件的getOrder()方法返回的值。 Sharing Objects between Fixtures。出于某种原因,学说试图以字母顺序(类名)加载下面的装置。

在下面的示例中,它必须加载Student,Subject和StudentSubject顺序,但是,它会尝试加载Student,StudentSubject和Subject,这会导致问题,因为StudentSubject在Student和Subject之前不能存在。协会:Student (1 -> N) StudentSubject (N <- 1) Subject

为什么会发生这种情况?

class StudentFixtures extends AbstractFixture implements FixtureInterface
{
    public function load(ObjectManager $manager)
    {
        ......
        $this->addReference('student-1', $student);
        ......
    }

    public function getOrder() { return 1; }
}

class SubjectFixtures extends AbstractFixture implements FixtureInterface
{
    public function load(ObjectManager $manager)
    {
        ......
        $this->addReference('subject-1', $subject);
        ......
    }

    public function getOrder() { return 2; }
}

class StudetSubjectFixtures extends AbstractFixture implements FixtureInterface
{
    public function load(ObjectManager $manager)
    {
        ......
        $student = $this->getReference('student-1');
        $subject = $this->getReference('subject-1');
        ......
    }

    public function getOrder() { return 3; }
}

错误:

Bc-MacBook-Pro:sport bc$ app/console doctrine:fixtures:load --no-interaction --no-debug
  > purging database
  > loading Football\FrontendBundle\DataFixtures\ORM\StudentFixtures
  > loading Football\FrontendBundle\DataFixtures\ORM\StudentSubjectFixtures
 [OutOfBoundsException]              
  Reference to: (mba) does not exist 

composer.json

"require-dev": {
    "sensio/generator-bundle": "~2.3",

    "doctrine/doctrine-fixtures-bundle": "2.2.0",
    "doctrine/data-fixtures": "1.1.1"
},

1 个答案:

答案 0 :(得分:16)

FixtureInterface的实例替换为OrderedFixtureInterface

例如:

use Doctrine\Common\DataFixtures\OrderedFixtureInterface;