问题
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):
- Remove all relations from my currently used entities.
- Delete the doctrime ORM file
src/Resources/config/doctrine
- Delete the class PHP file from
src/Entity
- 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