How to remove an entity class in a Symfony2 project

∥☆過路亽.° 提交于 2019-12-21 20:01:03

问题


When we initially designed our project, we had a couple of entities that to date are unused (and we don't plan to implement them in the near future). Ergo I want to remove them from my project. I would proceed like this (all steps manually performed):

  1. Remove all relations from my currently used entities.
  2. Delete the doctrime ORM file src/Resources/config/doctrine
  3. Delete the class PHP file from src/Entity
  4. Remove the table from the database

What I would like to know: Are there any routines (e.g. console commands) that may support this procedure? For example, if I run

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

after having removed all relations and deleted the files, that I get the SQL statement that removes the according table(s)?


回答1:


Your steps to delete an entity are OK.

You can't remove a table from Doctrine, as Doctrine doesn't know about it. Have a look at this question:

Deleting table using Doctrine2 and Symfony2




回答2:


Once you have removed the entities from your code, you can use the following console command to drop the tables:

php bin/console doctrine:schema:update --complete --dump-sql

Note the use of the --complete option.


Here are the relevant parts from the doctrine:schema:update help text:

Options:

--complete        If defined, all assets of the database which are not relevant to the current metadata will be dropped.
[...]

Help:

[...]

Finally, be aware that if the --complete option is passed, this task will drop all database assets (e.g. tables, etc) that are not described by the current metadata. In other words, without this option, this task leaves untouched any "extra" tables that exist in the database, but which aren't described by any metadata.

Hint: If you have a database with tables that should not be managed by the ORM, you can use a DBAL functionality to filter the tables and sequences down on a global level:

$config->setFilterSchemaAssetsExpression($regexp);



回答3:


to remove the table in symfony 3 you can just run a migration and the table not in use will be dropped from the DB:

php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate


来源:https://stackoverflow.com/questions/21903264/how-to-remove-an-entity-class-in-a-symfony2-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!