Search code examples
phpsymfonyentitysymfony4

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.


Solution

  • 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.