2016-07-06 14 views
0

Magento 2.1のバック・アンド・コントローラーで製品属性を削除するにはどうすればよいですか? Magentoの1にremoveAttribute magento 2

*だった:

$setup = Mage::getResourceModel('catalog/setup','catalog_setup'); 
$setup->removeAttribute('catalog_product','my_attribute'); 

EDIT: は私がインストール/アンインストール方法を使用していないでください。注目の質問を読む:

EDIT "backandコントローラ内の属性を削除し、" 2: 私は、あなたは、セットアップスクリプトの下に使用して属性を削除することができます

namespace Company\Module\Controller\Adminhtml\Shoptheme; //optional 

use Magento\Eav\Setup\EavSetup; 
use Magento\Eav\Setup\EavSetupFactory; 
use Magento\Framework\Setup\ModuleDataSetupInterface; 

class Removeattribute extends \Magento\Backend\App\Action { 
    private $dataSetup; 
    private $eavSetupFactory; 

    public function __construct(
     \Magento\Backend\App\Action\Context $context, 
     \Magento\Framework\Setup\ModuleDataSetupInterface $dataSetup, 
     \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory 
    ) { 
     $this->dataSetup = $dataSetup; 
     $this->eavSetupFactory = $eavSetupFactory; 
     parent::__construct($context); 
    } 

    public function execute() { 
     $eavSetup = $this->eavSetupFactory->create(['setup' => $this->dataSetup]); 
     $eavSetup->removeAttribute(
      \Magento\Catalog\Model\Product::ENTITY, 
      'prod_special_descr'); 

     $this->messageManager->addSuccess('attribute removed'); 
     $this->_redirect('admin/dashboard/'); 
    } 
} 
+0

はあなたが削除する必要がありますインストールスクリプトメソッドのみを使用する属性 –

答えて

0

に答える見つける:

<?php 

namespace Namespace\Company\Setup; 

use Magento\Eav\Setup\EavSetup; 
use Magento\Eav\Setup\EavSetupFactory; 
use Magento\Framework\Setup\InstallDataInterface; 
use Magento\Framework\Setup\ModuleContextInterface; 
use Magento\Framework\Setup\ModuleDataSetupInterface; 

class InstallData implements InstallDataInterface 
{ 
    private $eavSetupFactory; 

    public function __construct(EavSetupFactory $eavSetupFactory) 
    { 
     $this->eavSetupFactory = $eavSetupFactory; 
    } 

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 
    { 
     $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); 
     $eavSetup->removeAttribute(
      \Magento\Catalog\Model\Product::ENTITY, 
      'my_attribute'); 
    } 
}