Newtt
Newtt

Reputation: 6200

Add column to database in Yii

Hey I'm new to Yii framework.

My question here is how to add a column to a table within a database?

I've used addColumn() as follows:

Yii:app()->databasename->tablename->addColumn('tablename', 'newcolumn');

My problem here is what do I have to use to replace app(). By that I mean, should app() be replaced by database name, tablename, columnname or something else?

Thanks.

Upvotes: 0

Views: 4219

Answers (2)

Parham Doustdar
Parham Doustdar

Reputation: 2039

I don't know where you have found the Yii::app()->dbName->tableName->addColumn() from, but as far as I know, that piece of code will not work.

First of all, if you are trying to write something that will keep your table up-to-date (E.G. a way of versioning the database by adding/dropping columns/tables), consider using database migrations.

If you need this query for something else, you can use CDbCommand::addColumn(). For example:

Yii::app()->db->createCommand()->addColumn('user', 'name', 'varchar(64)');

Upvotes: 0

Samuel Liew
Samuel Liew

Reputation: 79113

This is how you should call the function:

$success = Yii::app()->db->createCommand()
               ->addColumn(string $table, string $column, string $type);

Simply replace the three variables in the addColumn method.

API: http://www.yiiframework.com/doc/api/1.1/CDbCommand#addColumn-detail

Upvotes: 1

Related Questions