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

霸气de小男生 提交于 2020-01-05 07:30:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!