将ManyToMany关联分解成2对OneToMany / ManyToOne(Doctrine) [英] Splitting a ManyToMany asociation into 2 pairs of OneToMany/ManyToOne (Doctrine)
问题描述
我有'Groupies'(因为group是一个保留的名字),而且我有公司。一个小组可以选择几个公司,并且相反(典型的ManyToMany关联)。
事情是:我需要坚持一些额外的数据,协会本身(我们称之为选择)。所以,ManyToMany被两对OneToMany / ManyToOne解密所取代,现在每个选择只有一个小组和一个公司。每个课程的学说元数据是:
Company.orm.yml:
Acme\AppBundle\Entity\Company:
type:entity
#fields ...
oneToMany:
choices:
targetEntity:Acme\AppBundle\Entity\Choice
mappedBy:company
Groupie.orm.yml:
Acme\AppBundle\Entity\Groupie:
类型:实体
#fields ...
oneToMany:
选择:
targetEntity:Acme\AppBundle\Entity\Choice
mappedBy:groupie
Choice.orm.yml:
Acme\AppBundle\Entity\Choice:
类型:实体
#fields ...
manyToOne:
公司:
targetEntity:Acme\AppBundle\Entity\Company
inversedBy:choices
manyToOne:
groupie:
targetEntity:Acme\AppBundle\Entity\Groupie
inversedBy:choices
问题是,当我运行comand:
php app / console doctrine:schema:update --dump -sql
似乎只能识别两个关系(小组)之一:
CREATE TABLE选项(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选择ADD CONSTRAINT FK_43CA0AD68D0C5D40 FOREIGN KEY(groupie_id)REFERENCES groupie(id);
我确实在做某些错误,但是我找不到如何将ManyToMany拆分成两个成对的OneToMany / ManyToOne详细说明。这样,似乎Choice.orm.yml中的最后一个manyToOne元数据会覆盖以前的元数据。事实上,如果我先写小组manyToOne,然后再写公司,那最后一个(公司)是选择表中唯一的外键!
谢谢提前!
你有你的答案在你的问题:
将相同缩进级别下的所有类型的关联组合起来。
Acme\AppBundle\Entity\Choice:
类型:实体
#fields ...
manyToOne:
公司:
targetEntity:Acme\AppBundle\Entity\Company
inversedBy:choices
groupie:
targetEntity:Acme\AppBundle\Entity\Groupie
inversedBy:choices
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. A Groupie may choose several Companies, and the same aplies in reverse (typical ManyToMany asociation).
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. The doctrine metadata for each class are:
Company.orm.yml:
Acme\AppBundle\Entity\Company:
type: entity
#fields...
oneToMany:
choices:
targetEntity: Acme\AppBundle\Entity\Choice
mappedBy: company
Groupie.orm.yml:
Acme\AppBundle\Entity\Groupie:
type: entity
#fields...
oneToMany:
choices:
targetEntity: Acme\AppBundle\Entity\Choice
mappedBy: groupie
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 somethig wrong, but I couldn't find how to split a ManyToMany into two pairs of OneToMany/ManyToOne asociations in detail. This way, it seems the last 'manyToOne' metadata in Choice.orm.yml overwrites the previous. 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!
Thanks in advance!
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
这篇关于将ManyToMany关联分解成2对OneToMany / ManyToOne(Doctrine)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!