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.

The required name parameter specifies a very brief description of the migration (e.g.<br />

create_news_table). As we will show in the following, the name parameter is used as part<br />

of a PHP class name. Therefore, it should only contain letters, digits and/or underscore<br />

characters.<br />

yiic migrate create create_news_table<br />

The above command will create under the protected/migrations directory a new file<br />

namedm101129_185401_create_news_table.php which contains the following initial code:<br />

class m101129_185401_create_news_table extends CDbMigration<br />

{<br />

public function up()<br />

{<br />

}<br />

}<br />

/*<br />

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

{<br />

}<br />

*/<br />

Notice that the class name is the same as the file name which is of the pattern<br />

m_, where refers to the UTC timestamp (in the format<br />

of yymmdd_hhmmss) when the migration is created, and is taken from the<br />

command's name parameter.<br />

The up() method should contain the code implementing the actual database migration,<br />

while the <strong>do</strong>wn()method may contain the code reverting what is <strong>do</strong>ne in up().<br />

Sometimes, it is impossible to implement <strong>do</strong>wn(). For example, if we delete table rows in<br />

up(), we will not be able to recover them in <strong>do</strong>wn(). In this case, the migration is called<br />

irreversible, meaning we cannot roll back to a previous state of the database.<br />

As an example, let's show the migration about creating a news table.

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

Saved successfully!

Ooh no, something went wrong!