简体   繁体   English

将ManyToMany关联分为2对OneToMany / ManyToOne(原则)

[英]Splitting a ManyToMany association into 2 pairs of OneToMany/ManyToOne (Doctrine)

I'll expose the case with the example (it'll be clearer): I have 'Groupies' (since group is a reserved name) and I have Companies. 我将通过示例(更清楚)展示该案例:我有“ Groupies”(因为group是保留名称),而我有Companies。 A Groupie may choose several Companies, and the same aplies in reverse (typical ManyToMany asociation). 一个Groupie可能会选择多个公司,而相同的公司则相反(典型的ManyToMany关联)。

The thing is: I need to persist some additional data wich are specific of the association itself (let's call it 'Choice'). 问题是:我需要保留一些特定于关联本身的其他数据(我们称其为“选择”)。 So, the ManyToMany is replaced by two pairs of OneToMany/ManyToOne asociations, and now each 'choice' has only one 'groupie' and one company. 因此,ManyToMany被两对OneToMany / ManyToOne关联所取代,现在每个“选择”只有一个“ Groupie”和一个公司。 The doctrine metadata for each class are: 每个类的学说元数据为:

Company.orm.yml: Company.orm.yml:

Acme\AppBundle\Entity\Company:
  type: entity
  #fields...
  oneToMany:
      choices:
        targetEntity: Acme\AppBundle\Entity\Choice
        mappedBy: company

Groupie.orm.yml: Groupie.orm.yml:

Acme\AppBundle\Entity\Groupie:
  type: entity
  #fields...
  oneToMany:
      choices:
        targetEntity: Acme\AppBundle\Entity\Choice
        mappedBy: groupie

Choice.orm.yml: Choice.orm.yml:

Acme\AppBundle\Entity\Choice:
  type: entity
  #fields...
  manyToOne:
    company:
      targetEntity: Acme\AppBundle\Entity\Company
      inversedBy: choices
  manyToOne:
     groupie:
      targetEntity: Acme\AppBundle\Entity\Groupie
      inversedBy: choices

The problem is, when I run the comand: 问题是,当我运行命令时:

php app/console doctrine:schema:update --dump-sql

only seems to recognize one of the two relationships (groupies): 似乎只能识别两个关系(群组)之一:

CREATE TABLE choice (id INT AUTO_INCREMENT NOT NULL, groupie_id INT DEFAULT NULL, creationDate DATE NOT NULL, orderNumber SMALLINT NOT NULL, numberOfAccounts SMALLINT NOT NULL, INDEX IDX_43CA0AD68D0C5D40 (choice_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE choice ADD CONSTRAINT FK_43CA0AD68D0C5D40 FOREIGN KEY (groupie_id) REFERENCES groupie (id);

I'm certainly doing something wrong, but I couldn't find how to split a ManyToMany into two pairs of OneToMany/ManyToOne associations in detail. 我当然做错了,但是我找不到如何将ManyToMany分成两对详细的OneToMany / ManyToOne关联的方法。 This way, it seems the last 'manyToOne' metadata in Choice.orm.yml overwrites the previous. 这样,Choice.orm.yml中的最后一个“ manyToOne”元数据似乎会覆盖前一个。 In fact, if I write first the groupie 'manyToOne' and then the company's, then this last one (company) is the only foreign key in the choice table! 实际上,如果我先编写分组“ manyToOne”,然后再编写公司的分组,那么最后一个(公司)是选择表中的唯一外键!

You have your answer in your question : 您的问题有答案:

Group all associations of same type under the same indentation level. 将相同类型的所有关联归入相同的缩进级别。

Acme\AppBundle\Entity\Choice:
   type: entity
   #fields...
   manyToOne:
     company:
      targetEntity: Acme\AppBundle\Entity\Company
      inversedBy: choices
     groupie:
      targetEntity: Acme\AppBundle\Entity\Groupie
      inversedBy: choices

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM