Yii迁移扩展自定义类

时间:2015-06-02 07:11:00

标签: php yii migration

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是扩展的默认类我需要扩展自定义类CustomCDbMigration

怎么做?配置中的一些设置?

1 个答案:

答案 0 :(得分:2)

为迁移创建自定义类。

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

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

转到控制台,保护路径,执行命令'yiic migrate create test'。 转到生成的迁移文件并更改为:

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

测试。在控制台'yiic migrate'中运行。

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

有关迁移的覆盖模板

//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...
}

检查迁移的新内容yiic migrate create test。新迁移将从/protected/migrations/template.php

获取内容