Add a column to an existing entity in Symfony

Add new Column in Existing entity on Symfony. I have the same problem are there . after I long research best solutions are there.

solution work on Symfony 4

Example:

Blog entity already created inside one name column are there and I want to add description column. so simple enter

php bin/console make:entity Blog

After run this command you want to add new column


Actually, using Doctrine does not make sense at all to do something like you suggested.

Doctrine is a ORM (Object-Relational Mapping) tool. It means that you want to abstract the database from your PHP code, you delegate database stuff to Doctrine. Doctrine does a wonderful job on that area.

As you want to keep your customers/peers updated with the latest version of the model, you should use the Doctrine Migrations ( http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html ). That's the way to manage database updates. Moreover, it gives you complete control on what to do when upgrading/downgrading the database. You could, e.g., set default values before you add the FK.

The steps for adding a new property on the class should be:

For Symfony 2:

  • modify the class by:

    • Acme\MyBundle\Entity\Customer and add the property you want;

      Acme\MyBundle\Entity\Customer

    • or modify the doctrine file:

      Acme/MyBundle/Resources/config/doctrine/customer.yml

  • run the console command (it will add the proper set/get in the class)

    php app/console doctrine:generate:entities AcmeMyBundle:Customer

  • run the console command

    php app/console doctrine:migrations:diff

  • run the console command (it will place a new file on app/DoctrineMigrations)

    php app/console doctrine:migrations:migrate

When you're deploying the new version of the code, all you got to do is update the source code and run the command above.

For Symfony 3:

  • modify the class by:

    • Acme\MyBundle\Entity\Customer and add the property you want;

      Acme\MyBundle\Entity\Customer

    • or modify the doctrine file:

      Acme/MyBundle/Resources/config/doctrine/customer.yml

  • run the console command (it will add the proper set/get in the class)

    php bin/console doctrine:generate:entities AcmeMyBundle:Customer

  • run the console command (update database)

    php bin/console doctrine:schema:update --force


You definitely DO want to open the PHP/XML/YML file where your entity is defined and add the column there. Then, you use the commandline and say

console doctrine:schema:update

That way, your entity definitions are in sync with the database and your database gets updated.