0

I have created a stub/template file I'd like to use for creating migrations

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

This is the code in I use to create migrations via Symfony Console component. I'm passing -t option as I want to generate a migration using the custom template I've created, but not sure how I can replace the DummyTableMigration with the class name I want to use. Do I need to pass it as an extra option within the 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);
ltdev
  • 4,037
  • 20
  • 69
  • 129

1 Answers1

1

Check the default template, it's using PHP style variables for the class name and various other aspects:

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

All available variables can be found in the Create command code: https://github.com/cakephp/phinx/blob/v0.11.0/src/Phinx/Console/Command/Create.php#L281-L288

ndm
  • 59,784
  • 9
  • 71
  • 110
  • Yeah, I've already seen this with the variables, I'm just not sure how I need to pass them in the ArrayInput – ltdev Nov 11 '19 at 20:22
  • @ltdev So the `DummyTableMigration` class in your question ist _not_ your template? The `$className` variable content is obtained from the `name` input option, so you just pass it in your array options like `'name' => 'TheClassName'`. If you want to obtain from somewhere dynamically, then you'll need to elaborate on that. – ndm Nov 11 '19 at 21:03