开发者

Adding new columns to an Existing Doctrine Model

开发者 https://www.devze.com 2023-03-04 01:45 出处:网络
First of all Hats of to StackOverflow for their great service and to you guys for taking your time to answer our questions.

First of all Hats of to StackOverflow for their great service and to you guys for taking your time to answer our questions.

I am using Doctrine ORM 1.2.4 with CodeIgniter 1.7.3. I created a Site with some required tables and pumped in with datas only to realize at a later point of time that a specific table needs to have one more column.

The way i created the tables was by writing the model as php classes which extend the Doctrine_Record.

Now i am wondering if i need to just add the column in the model that requires a new column in the setTableDefinition() method and recreate that table or is there any other way that easily does this. The former method i've mentioned requires me to drop the current table along with the datas and recreate the table which i do not wish. Since doctrine seems to be a very well architect-ed database framework, i believe it is lack of my knowledge but surely should exist a way to add new开发者_StackOverflow社区 columns easily.

PS: I am not trying to alter a column with relations to other tables, but just add a new column which is not related to any other table. Also i create the tables in the database using Doctrine::createTablesFromModels(); When i alter a table with a new column and run this method it shows errors.


Since you don't want to drop & recreate, use a Doctrine Migration.

The official docs here show many examples:

http://www.doctrine-project.org/projects/orm/1.2/docs/manual/migrations/en

Since you just want to add a field, look at their second code example as being the most relevant which is like this:

// migrations/2_add_column.php

class AddColumn extends Doctrine_Migration_Base
{
    public function up()
    {
        $this->addColumn('migration_test', 'field2', 'string');
    }

    public function down()
    {
        $this->removeColumn('migration_test', 'field2');
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消