Wizard
Wizard

Reputation: 11295

Yii migration extend custom class

class m150602_071107_naujassss extends CDbMigration
{
    public function up()
    {
    }

    public function down()
    {
        echo "m150602_071107_tests does not support migration down.\n";
        return false;
    }

    /*
    // Use safeUp/safeDown to do migration with transaction
    public function safeUp()
    {
    }

    public function safeDown()
    {
    }
    */
}

CDbMigration is default class to extend I need extend custom class CustomCDbMigration

How to do that ? Some settings in config ?

Upvotes: 1

Views: 804

Answers (1)

Danila Ganchar
Danila Ganchar

Reputation: 11312

Create custom class for migration.

/protected/components or /protected/extension/db (as you wish)
class CustomMigration extends CDbMigration
{

    protected function getMyVar()
    {
        return 'Custom migration';
    }
} 

Go to console, path to protected, execute command 'yiic migrate create test'. Go to the generated file of migration and change to:

class m150602_071449_test extends CustomMigration
{
    public function up()
    {
        echo $this->getMyVar();
        die();
    }
...

Testing. Run in console 'yiic migrate'.

Apply the above migration? (yes|no) [no]:yes
*** applying m150602_071449_test
Custom migration    <------- my function

For override template of migration:

//console config
'components'=>array(/*...*/),
'commandMap'=>array(
        'migrate'=>array(
            'class'=>'system.cli.commands.MigrateCommand',
            'migrationPath'=>'application.migrations',
            'migrationTable'=>'tbl_migration',
            'connectionID'=>'db',
            'templateFile'=>'application.migrations.template',
        ),
  ),

//template for migrations /protected/migrations/template.php
<?php
class {ClassName} extends CustomMigration
{
    public function up()
    {
    }

    //other methods...
}

Check new content of migration yiic migrate create test. New migration will be get content from /protected/migrations/template.php

Upvotes: 2

Related Questions