1

First off, I am completely new to Symfony2.

I created an entity -> created a table based on that entity -> created a form using the entity.

I have now realised I need to add a field to the form. So I did the following:

Added the new property -> Added the ORM annotations -> Generated the setters and getters -> ran "php app/console doctrine:schema:update"

This resulted in the following exception: "The table with name 'XXX' already exists"

So nothing was updated. Any idea what I did wrong? Below is the property I added to the entity:

/**
 * @var text
 *
 * @ORM\Column(name="description", type="text")
 *
 * @Assert\NotBlank(message="Please insert a description")
 * @Assert\Length(max=100)
 *
 */
private $description;
Developer1
  • 75
  • 1
  • 9
  • I have also tried this: http://stackoverflow.com/questions/14941358/add-a-column-to-an-existing-entity-in-symfony but that did not work either – Developer1 Aug 20 '15 at 12:46
  • what do you have inside of @ORM\Table() in your class annotations? – some_groceries Aug 20 '15 at 12:52
  • 1
    Are you sure you ran `php app/console doctrine:schema:update`? Firstly, you would need the flag `php app/console doctrine:schema:update --force` to actually run that command. Secondly, the exception you encountered sounds more like something you get when running `php app/console doctrine:schema:create` on an existing schema; e.g `SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'my_table' already exists`. – Darragh Enright Aug 20 '15 at 12:56
  • Yes sorry I have should have said I used the --force flag. and yes I did run that command. – Developer1 Aug 20 '15 at 13:31
  • The table name remains unchanged: @ORM\Table(name="category") – Developer1 Aug 20 '15 at 13:32
  • Always use doctrine:schema:update --dump-sql first just to make sure the schema will change as expected. Your error message indicates you have two sets of entities (perhaps two bundles, perhaps two entities within the same bundle) pointing to the same table. It's probably something you did before and it just never cropped up. Grep my_table in your project and see where it pops up. – Cerad Aug 20 '15 at 13:37
  • 1
    And why the drive by down vote? Seems like a perfectly reasonable question to me. Wish down voters would grow a pair and indicate why they do things. – Cerad Aug 20 '15 at 13:38
  • Since I am just starting out, everything is in AppBundle. There are no other entities by the same name as Category. Not sure what Grep is. Looking into it now. – Developer1 Aug 20 '15 at 13:51

2 Answers2

2

Try using Doctrine Migrations Bundle. What you are trying to do - make changes to a database you've already deployed - is called a "database migration." I've found this bundle to be very helpful.

Instead of running "app/console doctrine:schema:update", you'll run "app/console doctrine:migrations:diff" which will compare your database schema against your updated entity and generate the sql code to bring them back in sync.

Ian Phillips
  • 2,027
  • 1
  • 19
  • 31
0

I know this is an old question. But I write because it can be usefull for others.

Probably you are not using annotations in your project. Probably your project is confiured for use xml or yml.

you must to check your configuation at $proyect_home/app/config/config.yml and write in the orm zone:

orm:
  ....
  mappings:
    AppBundle:
      type: annotation
Curlas
  • 879
  • 5
  • 14