Programmatically add column to tables and modules

Hy everyone,

I’m developing a simple address book web application.

I would like to let user to add or remove columns to the anagraphic table but I don’t know how to do it in Yii.

Someone can help me or link me a tutorial or a page where I can find information?




Yii::app()->db->schema->addColumn( 'table_name', 'column_name', 'column type default xxx' );


Yii::app()->db->schema->dropColumn( 'tables_name', 'column_name' );



Thank you so much. I’ll try that code as soon as possible.

One more doubt: consider I build a GridView from a previusly created Model, based on a table called Users.

Now, with your code, I add a column to my db table. In this case will my gridview show the new inserted column?

There can be problems using crud operations trough ActiveRecord?

grid shows only columns passed in ‘columns’ configuration, so you must generate grid config dynamically in view based on table schema (check API reference for CTableSchema object)

Does anyone know how to implement this in Yii2? @redguy