Why this schema is generating a relation n:m

99封情书 提交于 2019-12-04 05:56:43

问题


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 }

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:

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?


回答1:


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

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

so add the foreignType: one parameter.



来源:https://stackoverflow.com/questions/17299823/why-this-schema-is-generating-a-relation-nm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!