現在の領域では削除操作は禁止されています
-
12-12-2019 - |
質問
skuごとに簡単な製品の削除操作を行うコマンドを作成したいと考えています。次のようなエラーが表示されます。管理領域を設定するにはどうすればよいですか?
[Magento\Framework\Exception\LocalizedException]
現在の領域では削除操作は禁止されています
<?php
namespace Sivakumar\Sample\Console;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
class DeleteSimpleProduct extends Command
{
protected $_product;
public function __construct(\Magento\Catalog\Model\Product $_product)
{
$this->_product =$_product;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('delete_simple_product')
->setDescription('Delete Simple Product')
->setDefinition($this->getOptionsList());
parent::configure();
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$errors = $this->validate($input);
if ($errors) {
throw new \InvalidArgumentException(implode("\n", $errors));
}
$product_id = $this->_product->getIdBySku($input->getOption('sku'));
$product=$this->_product->load($product_id);
$product->delete();
$output->writeln('<info>product deleted ' . $input->getOption('sku') . '</info>');
}
public function getOptionsList()
{
return [
new InputOption('sku', null, InputOption::VALUE_REQUIRED, 'SKU'),
];
}
public function validate(InputInterface $input)
{
$errors = [];
$required =['sku',];
foreach ($required as $key) {
if (!$input->getOption($key)) {
$errors[] = 'Missing option ' . $key;
}
}
return $errors;
}
}
di.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="delete_simple_product" xsi:type="object">Sivakumar\Sample\Console\DeleteSimpleProduct</item>
</argument>
</arguments>
</type>
</config>
解決
を使用する必要があるという Max の意見に同意します。 ProductRepositoryInterface::deleteById($sku)
, ただし、削除する権限を取得するには追加の変更を行う必要もあります。
管理領域は、次の構成を作成することでこれを処理することに注意してください。 app/code/Magento/Backend/etc/adminhtml/di.xml
<preference for="Magento\Framework\Model\ActionValidator\RemoveAction" type="Magento\Framework\Model\ActionValidator\RemoveAction\Allowed" />
の Magento\Framework\Model\ActionValidator\RemoveAction\Allowed
クラスは単純に返すことで権限チェックを防ぎます。 true
の中に isAllowed
方法。
上記の di.xml への変更を行わないと、 Magento\Framework\Model\ActionValidator\RemoveAction
クラスが使用されるため、次の場合を除き、削除リクエストは失敗します。 $this->registry->registry('isSecureArea')
true に設定されます。
いくつかのコンソールコマンドを作成しようとしているようですが、私はまだそれらにあまり慣れていません。そのため、現時点での最善の策は、削除操作を許可するようにレジストリを設定し、よりクリーンな解決策が見つかったら後でリファクタリングすることかもしれません。
$this->registry->register('isSecureArea', true)
他のヒント
最近、空のカテゴリを削除するコンソール コマンドを作成しているときにこの問題が発生しました。
別の回答で述べたように、登録する必要があります 'isSecureArea'
真実に。
これをコンソール コマンドで行うには、Magento\Framework egistry クラスをコンストラクターに渡す必要があります。
私の場合は次の方法で行いました。
public function __construct(CategoryManagementInterface $categoryManagementInterface, CategoryRepositoryInterface $categoryRepositoryInterface, Registry $registry)
{
$this->_categoryRepository = $categoryRepositoryInterface;
$this->_categoryManagement = $categoryManagementInterface;
$registry->register('isSecureArea', true);
parent::__construct();
}
そして、 execute
実際の削除にはリポジトリを使用しました。
$this->_categoryRepository->deleteByIdentifier($category->getId());
スクリプトを使用している場合は、以下のようにレジストリオブジェクトを作成してください。
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true);
.
詳しい説明はここをクリックしてください。 > http://www.pearlbells.co.uk/mass.-Delete-Magento-2-Cateries - プログラムによる/
1回のタイムスクリプトの場合は、OM
を使用できます。Chris o'tooleの答えの拡大。実際には複数のコマンドから、コマンドからカテゴリを削除する必要があります。最初は
を持っています$oRegistry->register('isSecureArea', true);
.
1つのコマンドが正常に機能しましたが、複数のコマンドで(コンストラクタ内)に置くと、コンパイル中にこのエラーが発生しました
レジストリキー "IsseCurearea"はすでに存在しています
レジストリキーの存在を最初にチェックしました
if($oRegistry->registry('isSecureArea') === null) {
$oRegistry->register('isSecureArea', true);
}
.
コンストラクタにそれを置くのが悪い形であるかどうかわかりませんが、エラーが発生した理由であるとします。あるいは、コマンド 'execute
メソッドから最初のスニペットを実行して逃げることができるはずです。繰り返しますが、私はベストプラクティスを考慮しているのかわからない...
製品での操作のためには、リポジトリを使用する必要があります。
Magento\Catalog\Model\ProductRepository
. ISSecureAraを設定する代わりに、このようにRemoveAction
の種類を上書きすることで、単一の種類のオブジェクトを削除することもできます。
<type name="Magento\Framework\Model\ActionValidator\RemoveAction">
<arguments>
<argument name="protectedModels" xsi:type="array">
<item name="salesOrder" xsi:type="null" /> <!--allow orders to be removed from front area-->
</argument>
</arguments>
</type>
.