Doctrine 多对多关系在我创建迁移时要创建两次表 [英] Doctrine many-to-many relationship wants to create a table twice when I create a migration

查看:8
本文介绍了Doctrine 多对多关系在我创建迁移时要创建两次表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在描述我的问题之前,如果我从遇到的错误开始,实际上可能会更清楚:

Before I describe my problem, it might actually make it clearer if I start with the error I'm getting:

$ ./app/console doc:mig:diff

  [DoctrineDBALSchemaSchemaException]                 
  The table with name 'user_media_area' already exists.  

这绝对是真的 - user_media_area 确实存在.我在之前的迁移中创建了它,但我不明白为什么 Symfony 会尝试再次创建表.

That's absolutely true - user_media_area does exist. I created it in a previous migration and I don't understand why Symfony is trying to create the table again.

我的问题与多对多关系有关.我有一个名为 user 的表、一个名为 media_area 的表和一个名为 user_media_area 的表.

My problem has something to do with a many-to-many relationship. I have a table called user, a table called media_area and a table called user_media_area.

这是我告诉 user 关于 media_area (Entity/User.php) 的代码:

Here's the code where I tell user about media_area (Entity/User.php):

/**
 * @ORMManyToMany(targetEntity="MediaArea", inversedBy="mediaAreas")
 * @JoinTable(name="user_media_area",
 *      joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@JoinColumn(name="media_area_id", referencedColumnName="id")}
 *      )
 */
private $mediaAreas;

这里是我告诉 media_area 关于 user (Entity/MediaArea.php) 的地方:

And here's where I tell media_area about user (Entity/MediaArea.php):

/** 
 * @ORMManyToMany(targetEntity="User", mappedBy="users")
 */
private $users;

有趣的是,如果我从 Entity/User.php 中删除 JoinTable 内容,./app/console 学说:迁移:差异 将再次工作:

What's interesting is that if I remove that JoinTable stuff from Entity/User.php, ./app/console doctrine:migrations:diff will work again:

/**
 * @ORMManyToMany(targetEntity="MediaArea", inversedBy="mediaAreas")
 */
private $mediaAreas;

但是,它有点偏离:它现在想要创建一个名为 mediaarea 的新表,而我不想要它.我的表已经存在,它被称为 media_area.

However, it's a little off: it now wants to create a new table called mediaarea, which I don't want. My table already exists and it's called media_area.

所以无论哪种方式,Symfony 都试图在我的 User 类中基于这个 ManyToMany 事物创建一个表,而问题消失的唯一原因是我删除 JoinTable 是它要创建的表的名称(mediaarea)不再与我的表的实际名称匹配(media_area).

So it looks like either way, Symfony is trying to create a table based on this ManyToMany thing in my User class, and the only reason the problem goes away when I remove the JoinTable is that the name of the table it wants to create (mediaarea) no longer matches the actual name of my table (media_area).

所以我的问题是:它为什么要创建一个新表?我做错了什么?

(我知道我的命名约定可能不正确.Symfony 和 Doctrine 的数据库示例令人沮丧地没有多术语列名,所以我并不总是知道我是否应该这样做 media_areamediaArea.)

(I know it's possible that my naming conventions are off. Symfony and Doctrine's database examples are frustratingly devoid of multi-term column names, so I don't always know if I'm supposed to do media_area or mediaArea.)

推荐答案

根据Association Mapping 说明在官方文档中,@JoinColumn@JoinTable 定义通常是可选的并且有合理的默认值,即:

According to the Association Mapping explanation on the official docs, the @JoinColumn and @JoinTable definitions are usually optional and have sensible default values, being:

name: "<fieldname>_id"
referencedColumnName: "id"

由此我们可以得出结论,您提出的两种实现之间确实没有具体区别.

From that we can conclude that there is really no concrete difference between the two implementations you presented.

但是,在迁移方面,创建表是一种非常常见且符合预期的行为.问题是表应该总是被删除并再次创建,这不会发生.

However, when it comes to migration, the creation of the table is a pretty common and expected behaviour. The thing is the table should always get deleted and created again, which is not happenning.

关于表名问题,Doctrine 2 的默认行为:

About the table name issue, the default behaviour of Doctrine 2 about this:

/**
 * @ORMManyToMany(targetEntity="MediaArea", inversedBy="mediaAreas")
 */
private $mediaAreas;

是尝试创建一个名为mediaarea的表.再次,完全正常.

Is to try and create a table called mediaarea. Again, perfectly normal.

如果你想为一个实体的表声明一个特定的名字,你应该这样做:

If you want to declare a specific name for the table of an entity, you should do this:

/**
 * @ORMTable(name="my_table")
 */
class Something

我不确定这是否对你有帮助,但我想它至少能让你走上正轨.

I'm not sure if that helps you at all, but I guess it puts you, at least, on the right track.

这篇关于Doctrine 多对多关系在我创建迁移时要创建两次表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆