为什么此模式生成关系 n:m [英] Why this schema is generating a relation n:m

查看:16
本文介绍了为什么此模式生成关系 n:m的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 schema.yml 文件(只是相关部分):

I've this schema.yml file (just relevant part):

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 Dotl:build-model 之后,我检查了 BaseSdrivingMaquina.class.php 类,我可以看到这个代码:

Then after I run the task symfony doctrine:build-model I check the class BaseSdrivingMaquina.class.php and I can see this code:

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

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

When I try to insert any record I get this error:

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

Couldn't call Doctrine_Core::set(), second argument should be an instance of Doctrine_Collection when setting one-to-many references.

这让我认为错误是关系.这篇文章与这个一个 谁能告诉我出了什么问题或我的错误在哪里?

Which make me think that the error is the relation. This post is related to this one can any tell me what's wrong or where is my mistake?

推荐答案

要有一对一的关系,你应该写

To have a one-to-one relation you should write

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

所以添加foreignType: one参数.

这篇关于为什么此模式生成关系 n:m的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆