向现有的学说模型添加新列

发布于 2024-11-05 20:38:17 字数 483 浏览 0 评论 0原文

首先感谢 StackOverflow 的优质服务,感谢你们花时间回答我们的问题。

我正在使用 Doctrine ORM 1.2.4 和 CodeIgniter 1.7.3。我创建了一个包含一些必需表的站点,并注入了数据,但后来才意识到特定表需要再有一列。

我创建表的方式是将模型编写为扩展 Doctrine_Record 的 php 类。

现在我想知道是否需要在模型中添加需要 setTableDefinition() 方法中的新列的列并重新创建该表,或者是否有任何其他方法可以轻松做到这一点。我提到的前一种方法要求我删除当前表和数据并重新创建我不希望的表。由于学说似乎是一个非常好的架构数据库框架,我相信它缺乏我的知识,但肯定应该存在一种轻松添加新列的方法。

PS:我并不是试图更改与其他表相关的列,而只是添加一个与任何其他表不相关的新列。我还使用 Doctrine::createTablesFromModels(); 在数据库中创建表当我用新列更改表并运行此方法时,它会显示错误。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2024-11-12 20:38:17

因为你不想放弃 &重新创建,使用 Doctrine Migration。

这里的官方文档显示了许多示例:

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

由于您只想添加一个字段,因此请查看他们最相关的第二个代码示例,如下所示:

// 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');
    }
}

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');
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文