【发布时间】:2023-04-04 19:31:01
【问题描述】:
我创建了自定义命令,并在其中添加了一个新属性。
protected function configure() : void
{
parent::configure();
$this
->setName('doctrine:migrations:generate:entitychange')
->addOption('db', null, InputOption::VALUE_REQUIRED, 'The database connection to use for this command.')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.')
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.')
->addOption('entity',null,InputOption::VALUE_OPTIONAL,
'Entity on which migration will be applied',
'User');
}
现在我想在迁移中的up() 和down() 函数中获得该参数的值。
例如:
public up(Schema $schema) : void
{
// How to get this
$attributOfMyInputValue = $getInputValueOfParam('entity')
if($attributOfMyInputValue === 'someValue') {
$sql = 'some query';
$this->addSql($sql);
} else {
...
}
}
【问题讨论】:
-
我不太确定是否要生成迁移,在这种情况下,您必须实际扩展您正在扩展的命令,但程度要深得多。 ...或者您是否想使用该参数执行迁移,那么您将扩展错误的命令,并且可能会覆盖教义迁移系统中的多个位置,这可能是没有人真正想做或做过的事情(只是在这里猜测,但我认为没有真正的原因)。 - 在不相关的注释php.net/manual/en/reserved.variables.argv.php 可能适用于肮脏的黑客。
-
我使用自定义模板并扩展
AbstractMigration以获得更多选项。就像在我的 Command 类中一样,我有输入变量,所以我可以做$input->getArgument('entity');我希望有一些简单的方法可以在迁移类中获得相同的值。