我遇到了多对多,自我引用的学说问题doctrine documentation
按照文档中的示例:
<entity
name="Application\Sonata\ProductBundle\Entity\Usersss"
table="usersss"
>
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<many-to-many field="myFriends" target-entity="Application\Sonata\ProductBundle\Entity\Usersss" inversedBy="friendsWithMe">
<join-table name="friends">
<join-columns>
<join-column id="user_id" referenced-column-name="id" />
</join-columns>
<inverse-join-columns>
<join-column id="friend_user_id" referenced-column-name="id"/>
</inverse-join-columns>
</join-table>
</many-to-many>
<many-to-many field="friendsWithMe" target-entity="Application\Sonata\ProductBundle\Entity\Usersss" mappedBy="myFriends"/>
</entity>
Symfony2应该生成类似的东西(正如文档中所写)
CREATE TABLE usersss (
id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
CREATE TABLE friends (
user_id INT NOT NULL,
friend_user_id INT NOT NULL,
PRIMARY KEY(user_id, friend_user_id)
) ENGINE = InnoDB;
ALTER TABLE friends ADD FOREIGN KEY (user_id) REFERENCES User(id);
ALTER TABLE friends ADD FOREIGN KEY (friend_user_id) REFERENCES User(id);
但是Symfony2尝试生成这个:
CREATE TABLE usersss (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE friends (usersss_id INT NOT NULL, PRIMARY KEY(usersss_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE usersss_usersss (usersss_source INT NOT NULL, usersss_target INT NOT NULL, INDEX IDX_A8965F1863AC54F9 (usersss_source), INDEX IDX_A8965F187A490476 (usersss_target), PRIMARY KEY(usersss_source, usersss_target)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE friends ADD CONSTRAINT FK_21EE7069A764047 FOREIGN KEY (usersss_id) REFERENCES usersss (id);
ALTER TABLE usersss_usersss ADD CONSTRAINT FK_A8965F1863AC54F9 FOREIGN KEY (usersss_source) REFERENCES usersss (id) ON DELETE CASCADE;
ALTER TABLE usersss_usersss ADD CONSTRAINT FK_A8965F187A490476 FOREIGN KEY (usersss_target) REFERENCES usersss (id) ON DELETE CASCADE;
如果我将inversedBy更改为inversed-by并将mappedBy更改为mapped-by,则mysql看起来像
CREATE TABLE usersss (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE friends (usersss_id INT NOT NULL, PRIMARY KEY(usersss_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE friends ADD CONSTRAINT FK_21EE7069A764047 FOREIGN KEY (usersss_id) REFERENCES usersss (id);
这仍然不像文档示例中那样
我无法理解为什么会出现如此奇怪的行为......
答案 0 :(得分:0)
编辑:实际上我发现了问题:因为你使用XML,你需要使用“inversed-by”而不是“inversedBy”和“mapped-by”而不是“mappedBy”。
编辑2 :另外,在联接列中将“id”替换为“name”。