Doctrine Fixture数据加载 - 插入时缺少值

时间:2015-11-27 14:27:46

标签: symfony orm doctrine-orm fixtures

我在我的symfony2 / doctrine2项目中创建了以下实体(学校和电路),其中包含多对一(A电路包含一个或多个学校)的双向关系。 以下是Schoool.orm.ymlcircuit.orm.yml定义

我试图为每个实体加载相应的数据夹具,如下所示:

 //Circuit Fixture
    public function load(ObjectManager $em) {
            $newCirco = new Circonscription();
            $newCirco->setId(1);
            $newCirco->setLibelle('Circuit1');

            $em->persist($newCirco);
            $em->flush();

            $this->addReference('circo1', $newCirco);
    }


    //School Fixture
     public function load(ObjectManager $em) {
            //Ecoles - Circo 1
            $ecole = new Etablissement();
            $ecole->setRne('0310211L');
            $ecole->setCirconscription($em->merge($this->getReference('circo1')));
            $ecole->setLibelle('FRANCE');
            $ecole->setAdresse('6 AVENUE DE Test');
            $ecole->setTelphone('05xxxxxxxx');
            $ecole->setEmail('xxxxxx@xxxxxxxxe.fr');

            $em->persist($ecole);
            $em->flush();
            $this->addReference('ecole', $ecole);
    }

但是,我从Doctrine获得了与完整性违规相关的NotNullConstraintViolationException

 An exception occurred while executing 'INSERT INTO etablissement (rne, libe
  lle, adresse, telphone, email, circo_id) VALUES (?, ?, ?, ?, ?, ?)' with pa
  rams ["0310241U", null, null, null, null, null]:
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'libelle' cann
  ot be null

虽然已在灯具中定义了值。 可能是这个错误的根源是什么?或者为什么由学说生成的插入不包含这些值?

1 个答案:

答案 0 :(得分:1)

在你的学校夹具中尝试更改setCirconscription()的参数:

 public function load(ObjectManager $em) {

        $ecole = new Etablissement();
        $ecole->setRne('0310211L');
        $ecole->setCirconscription($this->getReference('circo1'));

        /.../

}

不要忘记在Circuit Fixture

之前加载School Fixture