【问题标题】:addColumn yii migration positionaddColumn yii 迁移位置
【发布时间】:2012-09-12 10:22:04
【问题描述】:

我想在表格的第七位添加一列,我正在使用

$this->addColumn('table_name','column_name','type'); 

在末尾添加列。有什么办法可以提到添加列的地方吗?或任何后列关键字添加我的新列之后,例如,密码列。 我从Yii Doc了解了迁移

【问题讨论】:

    标签: php yii migration yii1.x


    【解决方案1】:

    这应该可行!

    $this->addColumn('table_name', 'column_name', 'type AFTER column6'); 
    

    示例:

    $this->addColumn('tbl_posts', 'email', 'VARCHAR(150) AFTER `name` ');
    $this->addColumn('tbl_posts', 'phone', 'string AFTER `email` ');
    

    【讨论】:

    • 谢谢!它做到了:) 我在文档中找不到这样做的方法,你还有什么其他关于 Yii 的参考吗?最后一个问题,当迁移类的名称似乎是 m120920_041119_update_users_about 我可以添加 $this->addColumn('users','about_ME','string AFTER password'); _ME 必须在类的名称中?
    • 它有效,但它在欺骗;当 Yii 编写 SQL 时,它只是简单地将 'type' 值“复制并粘贴”到 sql 中,并且随便地,语法 'type AFTER a_column' 生成有效的 SQL。当然,我很乐意从现在开始使用,感谢您向我们展示这种方法。
    • 在 Yii(和 PHP(和编程(和生活)))的世界里,很多东西都是“欺骗”的。
    • BEFORE 不起作用,因为它在 mysql 上不可用。它是第一个或之后。我不知道其他 rdbms。
    • 不是应有的方式。使用: $this->addColumn('{{%user}}', 'username', $this->string()->notNull()->unique()->after('id') );正如@gvanto所说的那样
    【解决方案2】:
    $this->addColumn('{{%user}}', 'username', 
                $this->string()->notNull()->unique()->after('id')
                );
    

    【讨论】:

    • 这是应该的方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多