PHP code example of andy87 / yii2-migrate-architect

1. Go to this page and download the library: Download andy87/yii2-migrate-architect library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andy87 / yii2-migrate-architect example snippets


use andy87\yii2\architect\components\controllers\ArchitectController;

return [
    // ...
    'controllerMap' => [
        // ...
    
        'architect' => ArchitectController::class,
        // ...
    ],
    // ...
];



use andy87\yii2\architect\components\controllers\ArchitectController;

return [
    // ...
    'controllerMap' => [
        // ...
    
        'architect' => [
            'class' => ArchitectController::class,
            'directoryTemplateMigrations' => '@app/path/to/migrations/template/',
            'migrateTemplateMapping' => [
                ArchitectController::SCENARIO_COLUMN_ADD => 'create_table_template',
                ArchitectController::SCENARIO_UPDATE => 'update_table_template',
                //,,,
            ],
            'snippetsMigrationFilename' => [
                ArchitectController::SCENARIO_COLUMN_ADD => 'create_table__%s',
                ArchitectController::SCENARIO_UPDATE => 'update_table__%s',
                //,,,
            ]
        ],
        // ...
    ],
    // ...
];



use andy87\yii2\architect\CreateTable;

/**
 * Class m240626_210742_create_table__role
 */
class m240626_210742_create_table__role extends CreateTable
{
    public string $scenario = self::SCENARIO_CREATE;

    /** @var string Название таблицы */
    public string $tableName = '{{%role}}';
    
    /** 
     * @var array Список для генерации внешних ключей.
     * Для примера добавлены два внешних ключа.
     */
    public array $foreignKeyList = [
        'module' => 'id', // fk--role-module_id--module-id
        'sub_module_id' => ['module' => 'id'], // fk--role-sub_module_id--module-id  
    ];

    /**
     * @return array
     */
    public function columns(): array
    {
        return  [
            'status' => $this->smallInteger()->notNull()->defaultValue(10),
            'key' => $this->string(32)->notNull()->unique(),
            'name' => $this->string(64)->notNull()->unique(),
            'priority' => $this->integer(4)->defaultValue(1),
            'module_id' => $this->integer(4)->notNull(),
            'sub_module_id' => $this->integer(4)->notNull(),
        ];
    }
}



use andy87\yii2\architect\CreateTable;

/**
 * Class m240626_210741_create_table__log
 */
class m240626_210741_create_table__log extends CreateTable
{
    public string $scenario = self::SCENARIO_CREATE;

    /** @var string Название таблицы */
    public string $tableName = 'log';
    
    /**
     * @return array
     */
    public function columns(): array
    {
        return  [
            self::COLUMN_ID => false,
            'text' => $this->string(32)->notNull()->unique(),
            self::COLUMN_UPDATED_AT => null,
        ];
    }
}



use andy87\yii2\architect\UpdateTable;


/**
 * Class m240626_210729_update_table__user
 */
class m240626_210729_update_table__user extends UpdateTable
{
    public string $scenario = self::SCENARIO_COLUMN_ADD;
    
    /** @var string Название таблицы */
    public string $tableName = 'user';

    /**
     * Список колонок для добавления
     * 
     * @return array
     */
    public function columnsListAdd(): array
    {
        return [
            'verification_token' => $this->string()->defaultValue(null)
        ];
    }
}



use andy87\yii2\architect\UpdateTable;


/**
 * Class m240626_210728_update_table__category
 */
class m240626_210728_update_table__category extends UpdateTable
{
    public string $scenario = self::SCENARIO_COLUMN_ADD;

    /** @var string Название таблицы */
    protected string $tableName = 'category';

    /** @var array Мэппинг внешних ключей */
    protected array $foreignKeyList = [
        'user'      => 'id', // fk--category-user_id--user-id
        'author_id' => ['user' => 'id'], // fk--category-author_id--user-id
        'parent_id' => ['category' => 'id'], // fk--category-parent_id--category-id
    ];
    
    /**
     * Список колонок для добавления
     *
     * @return array
     */
    public function columnsListAdd(): array
    {
        return [
            'user_id'   => $this->integer(8)->notNull(),
            'author_id' => $this->integer(8)->notNull()->after('user_id'),
            'parent_id' => $this->integer(8)->null()->after('id'),
        ];
    }
}



use andy87\yii2\architect\UpdateTable;
use app\common\models\sources\Role;

/**
 * Class m240626_210729_update_table__user
 */
class m240626_210729_update_table__user extends UpdateTable
{
    public string $scenario = self::SCENARIO_UPDATE;

    /** @var string Название таблицы */
    protected string $tableName = 'user';

    /**
     * Список колонок для изменения
     *
     * @return array
     */
    public function columnsListUpdate(): array
    {
        return [
            'auth_key' => $this->string(64)->notNull()->unique(),
            'author_id' => $this->integer(4)->notNull()->unique(),
        ];
    }
}



use app\common\models\sources\Role;
use andy87\yii2\architect\UpdateTable;

/**
 * Class m240626_210725_update_table__product
 */
class m240626_210725_update_table__product extends UpdateTable
{
    public string $scenario = self::SCENARIO_COLUMN_RENAME;

    /** @var string Название таблицы */
    protected string $tableName = 'product';

    /** @var array Список колонок для переименования */
    protected array $renameColumnList = [
        'old_price' => 'price',
        'new_price' => 'price_new',
    ];
}



use app\common\models\sources\Role;
use andy87\yii2\architect\UpdateTable;

/**
 * Class m240626_210735_update_table__service
 */
class m240626_210735_update_table__service extends UpdateTable
{
    public string $scenario = self::SCENARIO_COLUMN_REMOVE;

    /** @var string Название таблицы */
    protected string $tableName = 'service';

    /** @var array Список колонок для удаления */
    protected array $removeColumnList = [
        'comments' => null,
        'property' => null,
        'user_id' => ['user' => 'id'],
    ];
}
bash
  php yii architect