使用 Doctrine2 和 Symfony2 删除表格

2022-01-03 00:00:00 php symfony doctrine-orm

如何使用 Doctrine2 和 Symfony2 删除表?我已经生成了实体并更新了架构,现在我想删除这个结构.

How can I delete table using Doctrine2 and Symfony2? I've generated entities and updated schema, now I want to delete this structure.

推荐答案

不确定我是否正确理解了您的问题.您删除了一个实体并想从数据库中删除它生成的表?如果是这样:

Not sure if I understood your question correctly. You deleted an entity and want to delete also its generated table from database? If so:

你不能这样做,因为 Doctrine2 只关心它知道的表——这意味着那些由实体表示的表.由于您从应用程序中删除了一些实体,Doctrine 不再认为该表属于您的应用程序.存在同一个数据库中存在不同应用的不同表的情况.如果 Doctrine 删除它们只是因为它对它们一无所知,那将毫无意义.这将是种族主义......但反对桌子.

You can't do it, because Doctrine2 only cares about tables it knows - that means the ones that are represented by Entities. Since you deleted some Entity from your application, Doctrine no longer thinks the table belongs to your application. There are situations when there are different tables of different applications in the same database. It wouldn't make sense if Doctrine deleted them just because it doesn't know anything about them. It would be racist... but against tables.

如果您只想以编程方式删除表,您可以使用原始查询.据我所知,Doctrine 没有任何删除表的方法.或者,您也可以手动完成.

If you just want to drop tables programmatically, you can use raw query. As far as I know, Doctrine doesn't have any method for dropping tables. Or as an alternative, you can do it by hand.

相关文章