为什么这个模式生成关系n:m

时间:2013-06-25 14:19:27

标签: symfony1 symfony-1.4 doctrine-1.2

我的schema.yml文件(只是相关部分):

SdrivingMaquina:
  actAs: 
    Timestampable: ~
  columns:
    idmaquina: { type: integer(8), autoincrement: true, notnull: true, primary: true }
    idempresa: { type: integer(4), notnull: true }
    patente: { type: string(12), notnull: true }
  relations:
    Empresa: { local: idempresa, class: SdrivingEmpresa, type: one, foreignType: one, foreignAlias: MaquinaEmpresa, onDelete: CASCADE, onUpdate: CASCADE }
SdrivingMaquinaEmisor:
  actAs: 
    Timestampable: ~
  columns:
    idmaquinaemisor: { type: integer(8), primary: true, autoincrement: true }
    idmaquina: { type: integer(8), notnull: true }
    idemisor: { type: integer(8), notnull: true }
  relations:
    SdrivingEmisor: { onDelete: CASCADE, local: idemisor, foreign: idemisor, type: one }
    SdrivingMaquina: { onDelete: CASCADE, local: idmaquina, foreign: idmaquina, type: one }

然后在我运行任务symfony doctrine:build-model后,我检查了班级BaseSdrivingMaquina.class.php,我可以看到这段代码:

public function setUp()
    {
        parent::setUp();
        $this->hasOne('SdrivingEmpresa as Empresa', array(
             'local' => 'idempresa',
             'foreign' => 'id',
             'onDelete' => 'CASCADE',
             'onUpdate' => 'CASCADE'));

        $this->hasOne('SdrivingEmpresa', array(
             'local' => 'idempresa',
             'foreign' => 'idempresa'));

        $this->hasMany('SdrivingMaquinaEmisor', array(
             'local' => 'idmaquina',
             'foreign' => 'idmaquina'));

        $timestampable0 = new Doctrine_Template_Timestampable();
        $this->actAs($timestampable0);
    }

当我尝试插入任何记录时,我收到此错误:

  

无法调用Doctrine_Core :: set(),第二个参数应该是   设置一对多引用时的Doctrine_Collection实例。

这让我觉得错误是关系。这篇文章与one有关,可以告诉我哪些错误或错误在哪里?

1 个答案:

答案 0 :(得分:1)

要建立一对一的关系,你应该写

SdrivingMaquina: { onDelete: CASCADE, local: idmaquina, foreign: idmaquina, type: one, foreignType: one }

所以添加foreignType: one参数。