Daniel
Daniel

Reputation: 418

Symfony - Add a column to a table without losing already generated classes

How can I add a column to a Database table without overwriting the classes already generated? (Doctrine) What files do I have to edit?

If I simply add the column in the database, I can't use the set and get functions of Doctrine ORM.

Upvotes: 1

Views: 4188

Answers (3)

Nacef
Nacef

Reputation: 433

Use doctrine migrations. It allows you to modify your schema, update the database and your model without also losing the existing data in your database (in case it is relevant).

http://www.symfony-project.org/doctrine/1_2/en/07-Migrations

Applicable for symfony 1.4 too

Upvotes: 8

J0HN
J0HN

Reputation: 26921

Doctrine 1.2:

Go to models folder, open generated class that reflects table to which you have added a column. Add

$this->hasColumn('id', 'integer', 8, array(
         'type' => 'integer',
         'autoincrement' => true,
         'primary' => true,
         'length' => '8',
         ));

to setTableDefinition method.

Note, that your changes will be overwritten on generate-models, so make sure you populate it to YAML/DB schema

See Doctrine Models Definition Manual for reference.

Doctrine 2

Samples given for Annotations Driver, see Doctrine2 manual for other XML and YAML drivers Just add new property to your @Entity class with @Column annotation on it:

/** @Column(type="integer", name="new_column") */
protected $new_column;

Upvotes: 2

Gerry
Gerry

Reputation: 6012

You should never edit the base classes (BaseFoo.class.php) as these get overwritten everytime you generate the models from the schema. The other files are never overwritten so it's safe to edit.

Upvotes: 4

Related Questions