Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine fixtures won't get loaded in given order, instead, they get loaded in alphabetical order

As far as I know, fixtures will be loaded in given order, based on the value returned in getOrder() method of each fixture files. Sharing Objects between Fixtures. For some reason, doctrine tries to load fixtures below in alphabetical order (class name) instead.

In example below, it has to load in Student, Subject and StudentSubject order however, it tries to load in Student, StudentSubject and Subject which causes problem because StudentSubject cannot exist before Student and Subject. Association: Student (1 -> N) StudentSubject (N <- 1) Subject

Any reason why this is happening?

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

Error:

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"
},
like image 268
BentCoder Avatar asked Jun 06 '15 22:06

BentCoder


1 Answers

Replace instances of FixtureInterface with OrderedFixtureInterface

for example:

use Doctrine\Common\DataFixtures\OrderedFixtureInterface;

like image 98
geoB Avatar answered Nov 17 '22 11:11

geoB