Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine ORM -Remove attribute from entity?

Very simple thing I want to do, however I fail to find a solution. I have started to develop my app with Symfony4 bundled with Doctrine. At the beginning I have designed database model but during development I have realized original solution is wrong way for what I want to do. Right now, I want to remove an attribute from entity. In plain SQL I would do ALTER TABLE table DROP COLUMN column then recreate it again with new parameters. However this solution gave me an error in Doctrine, so I have changed PHP model as well. Again, another error. Okay, so this does not look like the way I want to go. Is there any solution to my problem, except digging too deep in Doctrine? Best would be something simple like described in the SQL command above?

like image 999
JohnyProkie Avatar asked Aug 02 '18 09:08

JohnyProkie


1 Answers

When it comes to doctrine with symfony you should forget about tables, columns etc.. and start thinking with objects (otherwise there's no point in using doctrine for abstraction). For your problem you should simply remove the attribute in your entity.

Then You should use :

php bin/console doctrine:migrations:diff

Which will generate a migration file under the /migrations folder. This file will contain the SQL that doctrine will execute. It allows you to verify if the query is what you expect, if not you can directly modify it in that file or adapt your entity and generate a new migration file.

When you are happy with it, you can execute it with :

php bin/console doctrine:migrations:migrate
like image 106
Elbarto Avatar answered Nov 12 '22 08:11

Elbarto