2012-09-20 31 views
24

我想在表中的第七位增加一列,我使用addColumn警予的遷移位置

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

末將此列。有什麼方法可以提到添加列的地方嗎?或者之後的任何列關鍵字添加我的新列,作爲exapmle,密碼列。 我已經學會了從Yii Doc

回答

40

aboout遷移這應該工作!

$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` '); 
+1

謝謝!它確實:)我找不到在文檔中做到這一點的方式,是否還有其他你提到的Yii。只有最後一個問題,當遷移類的名字似乎是m120920_041119_update_users_about時,我可以添加$ this-> addColumn('users','about_ME','string AFTER password'); _ME必須以班級的名義? –

+0

它的作品,但它的作品欺騙;當Yii組成SQL時,它只是將'type'值'複製並過去'到sql中,隨後,語法'type AFTER a_column'生成有效的SQL。當然,我很高興從現在開始使用,並感謝向我們展示這種方法。 – realtebo

+2

很多東西是由Yii(和PHP(以及編程(和生活))的世界中的「欺騙」所造成的。 –

15
$this->addColumn('{{%user}}', 'username', 
      $this->string()->notNull()->unique()->after('id') 
      ); 
+3

這工作正常,它的優雅。謝謝! –

+1

這是*它應該是的方式。 – christian