问题
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.
回答1:
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.
回答2:
You can follow a similar pattern:
- Change the field (annotation) in your
User
-entity from name to username - Run
bin/console doctrine:migrations:diff
- 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
来源:https://stackoverflow.com/questions/57229153/update-an-entity-in-symfony-4