【问题标题】:Phinx - custom migration template passing params to the templatePhinx - 将参数传递给模板的自定义迁移模板
【发布时间】:2019-11-08 19:18:41
【问题描述】:

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

<?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);

【问题讨论】:

  • 您是否尝试过使用模板标记,例如在cakephp/migrations/src/Template/Bake/config/skeleton.ctp ?

标签: php symfony cakephp console-application phinx


【解决方案1】:

检查默认模板,它使用 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

【讨论】:

  • 是的,我已经看到了这个变量,我只是不确定我需要如何在 ArrayInput 中传递它们
  • @ltdev 所以你问题中的DummyTableMigration不是你的模板吗? $className 变量内容是从name 输入选项中获得的,因此您只需将它传递到您的数组选项中,例如'name' =&gt; 'TheClassName'。如果您想从某处动态获取,则需要详细说明。
猜你喜欢
  • 2011-11-10
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
相关资源
最近更新 更多