我們需要在module目錄下面建一個Setup文件夾
這個文件件存放的是執(zhí)行setup:upgrade命令的時候會執(zhí)行的一些代碼
主要是數(shù)據(jù)庫以及一些配置
我們要新建一個product的attribute我們只需要新建一個這樣的目錄結(jié)構(gòu)
image.png
然后我們寫入代碼
<?php
namespace TmobLabs\Tappz\Setup;
/**
* Created by PhpStorm.
* User: bardiaafshin
* Date: 8/28/16
* Time: 11:22 AM
*/
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();
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,//對應(yīng)要添加attribute的entity 類
'material',//添加的attribute的名稱
[
'group'=>'Genneral',
'type' => 'varchat',//存放的類型
'backend' => '',
'frontend' => '',
'label' => 'Material',
'input' => '',
'source' => '',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
}
}