Symfony - Add a column to a table without losing a

2019-08-02 17:36发布

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.

3条回答
聊天终结者
2楼-- · 2019-08-02 18:03

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楼-- · 2019-08-02 18:17

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

查看更多
聊天终结者
4楼-- · 2019-08-02 18:19

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;
查看更多
登录 后发表回答