Magento 2 CLI命令在添加构造函数后停止工作



我在magento 2中制作了一个CLI命令来处理CSV文件,但当我添加__constructor()以使用ProductRepository通过CSV管理我的产品时,我的终端开始显示以下错误:

There are no commands defined in the "xxx" namespace.

这是我的代码:

<?php
namespace XxxCommandConsoleCommand;
use MagentoCatalogHelperOutputTest;
use MagentoCatalogModelProductRepository;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleOutputOutputInterface;
/**
* Class importProducts
*/
class importProducts extends Command
{
const FILE_NAME = 'file_name';
const COLS = 'cols';

protected $pr;
public function __construct(ProductRepository $pr, string $name = null)
{
$this->pr = $pr;
parent::__construct($name);
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('xxx:isp');
$this->setDescription('Imports products.');
$this->addOption(
self::FILE_NAME,
'f',
InputOption::VALUE_REQUIRED,
'File name'
);
$this->addOption(
self::COLS,
'c',
InputOption::VALUE_REQUIRED,
'Number of columns'
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$fileName = $input->getOption('file_name');
if (!isset($fileName)) {
$output->writeln('Please provide file name!');
return 0;
}
$file = file('pub/command/import2.csv');
$csv = array_map('str_getcsv', $file);
return 1;
}
}

如果你不能想出任何其他解决方案,这里有一个快速的"破解";使其发挥作用:

<?php
namespace XxxCommandConsoleCommand;
use MagentoCatalogHelperOutputTest;
use MagentoCatalogModelProductRepository;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleOutputOutputInterface;
use MagentoFrameworkAppObjectManager;

/**
* Class importProducts
*/
class importProducts extends Command
{
const FILE_NAME = 'file_name';
const COLS = 'cols';

protected $pr;
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('xxx:isp');
$this->setDescription('Imports products.');
$this->addOption(
self::FILE_NAME,
'f',
InputOption::VALUE_REQUIRED,
'File name'
);
$this->addOption(
self::COLS,
'c',
InputOption::VALUE_REQUIRED,
'Number of columns'
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// this is not the recommended way to do this!
$this->pr = ObjectManager::getInstance()->get(ProductRepository::class);
$fileName = $input->getOption('file_name');
if (!isset($fileName)) {
$output->writeln('Please provide file name!');
return 0;
}
$file = file('pub/command/import2.csv');
$csv = array_map('str_getcsv', $file);
return 1;
}
}

最新更新