addColumn yii migration position
Asked Answered
B

2

38

I want to add a column at the seventh place in the table, I am using

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

adds the column at the end. Is there any way where I can mention the place to add column? Or any after column keyword to add my new column after, for exapmle, password column. I have learnt aboout migration from Yii Doc

Blondy answered 20/9, 2012 at 4:27 Comment(0)
H
45

This should work!

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

examples:

$this->addColumn('tbl_posts', 'email', 'VARCHAR(150) AFTER `name` ');
$this->addColumn('tbl_posts', 'phone', 'string AFTER `email` ');
Harden answered 20/9, 2012 at 5:52 Comment(5)
Thanks! It did :) I couldn't find the way to do it in the doc, is there anything else that you refer for Yii. and just one last question, when the name of migration class appears to be m120920_041119_update_users_about can I add $this->addColumn('users','about_ME','string AFTER password'); The _ME has to be in the name of the class?Blondy
It works, but it works tricking; when Yii compose the SQL, it simply 'copy and past' the 'type' value into sql, and, casually, the syntax 'type AFTER a_column' generate valid SQL. Of course, I'm happy to use from now, and thanks for showing us this method.Trimming
A lot of stuff is made by "tricking" in Yii (and PHP (and programming (and life)))'s world.Dulcie
BEFORE doesn't work since it's not available on mysql. It's FIRST, or AFTER. I don't know with other rdbms.Sklar
this IS NOT the way it should be. use: $this->addColumn('{{%user}}', 'username', $this->string()->notNull()->unique()->after('id') ); as it was said by @DeepfreezePermute
D
54
$this->addColumn('{{%user}}', 'username', 
            $this->string()->notNull()->unique()->after('id')
            );
Deepfreeze answered 20/6, 2016 at 1:42 Comment(2)
THIS is the way it should be.Permute
Note that after method only support MySQL, Oracle and Cubrid.Idolla
H
45

This should work!

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

examples:

$this->addColumn('tbl_posts', 'email', 'VARCHAR(150) AFTER `name` ');
$this->addColumn('tbl_posts', 'phone', 'string AFTER `email` ');
Harden answered 20/9, 2012 at 5:52 Comment(5)
Thanks! It did :) I couldn't find the way to do it in the doc, is there anything else that you refer for Yii. and just one last question, when the name of migration class appears to be m120920_041119_update_users_about can I add $this->addColumn('users','about_ME','string AFTER password'); The _ME has to be in the name of the class?Blondy
It works, but it works tricking; when Yii compose the SQL, it simply 'copy and past' the 'type' value into sql, and, casually, the syntax 'type AFTER a_column' generate valid SQL. Of course, I'm happy to use from now, and thanks for showing us this method.Trimming
A lot of stuff is made by "tricking" in Yii (and PHP (and programming (and life)))'s world.Dulcie
BEFORE doesn't work since it's not available on mysql. It's FIRST, or AFTER. I don't know with other rdbms.Sklar
this IS NOT the way it should be. use: $this->addColumn('{{%user}}', 'username', $this->string()->notNull()->unique()->after('id') ); as it was said by @DeepfreezePermute

© 2022 - 2024 — McMap. All rights reserved.