问题
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.
回答1:
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
回答2:
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.
回答3:
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;
来源:https://stackoverflow.com/questions/7146747/symfony-add-a-column-to-a-table-without-losing-already-generated-classes