Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

php - Add a column to an existing entity in Symfony

I've been playing around with Symfony on my web server and I've been creating entity with doctrine for my database. I wanted to add a column to one of these entity... I wanted to do something like:

php app/console doctrine:modify:entity

Now I know that this command doesn't exists, but is there a way (without doing a whole migration) to simply add a column.

P.S. I know I could open the php file and textually add the column there and then update the schema, but I'm distributing this to some clients and I like a more "command-line-like" approach.

question from:https://stackoverflow.com/questions/14941358/add-a-column-to-an-existing-entity-in-symfony

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

    • AcmeMyBundleEntityCustomer and add the property you want;

      AcmeMyBundleEntityCustomer

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

    • AcmeMyBundleEntityCustomer and add the property you want;

      AcmeMyBundleEntityCustomer

    • 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...