0

我创建了一个存根/模板文件,我想用它来创建迁移

<?php

use Phinx\Migration\AbstractMigration;

class DummyTableMigration extends AbstractMigration
{
    public function up()
    {
        // Create the table
        $table = $this->table('table_name');

        $table->addColumn('column_name', 'string', ['limit' => 255])
              ->create();
    }

    public function down()
    {
        $this->table('table_name')->drop()->save();
    }
}

这是我用来通过 Symfony 控制台组件创建迁移的代码。我正在传递-t选项,因为我想使用我创建的自定义模板生成迁移,但不确定如何将 替换为DummyTableMigration我想要使用的类名。我是否需要将其作为额外选项传递给ArrayInput?

$phinx = new PhinxApplication();
    $input = new ArrayInput([
        'command' => 'create',
        'name' => $input->getArgument('name'),
        '-c' => './config/phinx.php',
        '-t' => '../../Console/stubs/migrations/customTemplateMigration.stub'),
    ]);

    return $phinx->find('create')->run($input, $output);
4

1 回答 1

0

检查默认模板,它使用 PHP 样式变量作为​​类名和其他各种方面:

<?php
$namespaceDefinition
use $useClassName;

class $className extends $baseClassName
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    addCustomColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Any other destructive changes will result in an error when trying to
     * rollback the migration.
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {

    }
}

https://github.com/cakephp/phinx/blob/v0.11.0/src/Phinx/Migration/Migration.template.php.dist

所有可用的变量都可以在Create命令代码中找到:https ://github.com/cakephp/phinx/blob/v0.11.0/src/Phinx/Console/Command/Create.php#L281-L288

于 2019-11-11T10:38:17.030 回答