TaouBen
TaouBen

Reputation: 1315

Update an entity in Symfony 4?

My question is simple, but I can't find a fine answer to it, I had an entity created by a command line :

php bin/console make:entity

this entity is User that has few attribute ( name - email - password )

After inserting the fields, I migrated, so my table has been created in the database using those commandlines :

php bin/console make:migration

php bin/console doctrine:migration:migrate

But now I want to just change the name to a username but I don't know how to do it.

I did not find anything in the documentation, so any help would be much appreciated.

Upvotes: 3

Views: 14653

Answers (2)

Abbas Afsharfarnia
Abbas Afsharfarnia

Reputation: 201

Be careful: After any changes in your entities you must generate new migration file. In fact this file contains all of the changes which must be done on your database to be update. To generate this file (new migration version) you can follow these commands:

$ bin/console doctrine:cache:clear-metadata
$ bin/console doctrine:migrations:diff

After above commands you generated your new version file successfully, now if you run following command, you can see you have a new and not executed version file:

$ bin/console doctrine:migrations:status

Finally, to execute the new version file and update your database, you must run the following command:

$ bin/console doctrine:migrations:migrate --all-or-nothing

now your database is update and in the table migration_versions you can see that new version has been added.

Be Successful.

Upvotes: 8

dbrumann
dbrumann

Reputation: 17166

You can follow a similar pattern:

  1. Change the field (annotation) in your User-entity from name to username
  2. Run bin/console doctrine:migrations:diff
  3. Run bin/console doctrine:migrations:migrate

The doctrine:migrations:diff command should detect that the field changed and create a corresponding SQL query for you, which is then stored in a DoctrineMigrations-file right next to the original one.

The doctrine:migrations:migrate detects this new migration and that it was not previously executed, by checking against the doctrine_versions table, where all migration versions that were executed are stored.

See also: https://www.doctrine-project.org/projects/doctrine-migrations/en/2.1/reference/generating-migrations.html#diffing-using-the-orm

Upvotes: 6

Related Questions