symfony - Doctrine 固定装置不会按给定顺序加载,而是按字母顺序加载

标签 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"
},

最佳答案

FixtureInterface 的实例替换为 OrderedFixtureInterface

例如:

使用 Doctrine\Common\DataFixtures\OrderedFixtureInterface;

关于symfony - Doctrine 固定装置不会按给定顺序加载,而是按字母顺序加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30688317/

相关文章:

php - Doctrine 2 无法运行 SELECT 查询问题

Symfony2 数据装置

mysql - DQL 和等效 SQL 不返回相同数量的结果集

symfony - Doctrine2 查询,从不同的 Symfony2 包中选择多个实体

php - 如何在 Symfony 2 中返回实体的本地化属性

动态内容的 Symfony 翻译

Symfony2 Doctrine 事件监听器 prePersist 不起作用

php - Symfony 3 MySQL 驱动程序 'could not find driver'

symfony - 依赖不存在的服务 "doctrine.orm.default_entity_manager"

php - Doctrine2,映射 "inherited"表