Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an entity class in a Symfony2 project

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)?

like image 256
Gottlieb Notschnabel Avatar asked Dec 25 '22 13:12

Gottlieb Notschnabel


2 Answers

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);
like image 53
Sébastien Avatar answered Jan 04 '23 16:01

Sébastien


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
like image 23
fingerman Avatar answered Jan 04 '23 18:01

fingerman