20.11.2014 Views

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

class m101129_185401_create_news_table extends CDbMigration<br />

{<br />

public function up()<br />

{<br />

$this->createTable('tbl_news', array(<br />

'id' => 'pk',<br />

'title' => 'string NOT NULL',<br />

'content' => 'text',<br />

));<br />

}<br />

}<br />

public function <strong>do</strong>wn()<br />

{<br />

$this->dropTable('tbl_news');<br />

}<br />

The base class CDbMigration provides a set of methods for manipulating data and<br />

schema of a database. For example, CDbMigration::createTable will create a database<br />

table, while CDbMigration::insert will insert a row of data. These methods all use the<br />

database connection returned by CDbMigration::getDbConnection(), which by default<br />

returns <strong>Yii</strong>::app()->db.<br />

Info: You may notice that the database methods provided by CDbMigration are very<br />

similar to those in CDbCommand. Indeed they are nearly the same except that<br />

CDbMigration methods will measure the time used by their methods and print some<br />

messages about the method parameters.<br />

Applying Migrations<br />

To apply all available new migrations (i.e., make the local database up-to-date), run the<br />

following command:<br />

yiic migrate<br />

The command will show the list of all new migrations. If you confirm to apply the<br />

migrations, it will run theup() method in every new migration class, one after another, in the<br />

order of the timestamp value in the class name.<br />

After applying a migration, the migration tool will keep a record in a database table named<br />

tbl_migration. This allows the tool to identify which migrations have been applied and<br />

which are not. If the tbl_migrationtable <strong>do</strong>es not exist, the tool will automatically create it in<br />

the database specified by the db application component.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!