Why this schema is generating a relation n:m

2019-03-04 00:16发布

问题:

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.