Magento 2 - 如何基于默认创建属性集
Magento 2 - How to create an attribute set based on default
我目前有以下创建新属性集的代码:
use Magento\Framework\ObjectManagerInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class AttributeModel extends \Magento\Framework\Model\AbstractModel
{
const ENTITY_TYPE = \Magento\Catalog\Model\Product::ENTITY;
protected $_objectManager;
protected $_moduleDataSetup;
protected $_eavSetupFactory;
public function __construct(
ObjectManagerInterface $objectManager,
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory,
) {
$this->_objectManager = $objectManager;
$this->_moduleDataSetup = $moduleDataSetup;
$this->_eavSetupFactory = $eavSetupFactory;
}
public function createSet($name)
{
$eavSetup = $this->_eavSetupFactory->create([
'setup' => $this->_moduleDataSetup
]);
$eavSetup->addAttributeSet(self::ENTITY_TYPE, $name, 0);
}
}
但是该集没有分配给它的属性。我如何根据默认设置创建集合,以便它预先填充基本属性?
非常感谢任何帮助。
事实证明,AttributeSetManagementInterface 模型有一个创建函数,它采用一个可选的骨架 ID,新集合可以基于该 ID。我最终使用 objectManager 进行快速修复,我确信有更好的方法。
下面对上面的 OP 代码进行扩展:
public function createSet($name)
{
$eavSetup = $this->_eavSetupFactory->create([
'setup' => $this->_moduleDataSetup
]);
$defaultId = $eavSetup->getDefaultAttributeSetId(self::ENTITY_TYPE);
$model = $this->_objectManager
->create('Magento\Eav\Api\Data\AttributeSetInterface')
->setId(null)
->setEntityTypeId(4)
->setAttributeSetName($name);
$this->_objectManager
->create('Magento\Eav\Api\AttributeSetManagementInterface')
->create(self::ENTITY_TYPE, $model, $defaultId)
->save();
}
我目前有以下创建新属性集的代码:
use Magento\Framework\ObjectManagerInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class AttributeModel extends \Magento\Framework\Model\AbstractModel
{
const ENTITY_TYPE = \Magento\Catalog\Model\Product::ENTITY;
protected $_objectManager;
protected $_moduleDataSetup;
protected $_eavSetupFactory;
public function __construct(
ObjectManagerInterface $objectManager,
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory,
) {
$this->_objectManager = $objectManager;
$this->_moduleDataSetup = $moduleDataSetup;
$this->_eavSetupFactory = $eavSetupFactory;
}
public function createSet($name)
{
$eavSetup = $this->_eavSetupFactory->create([
'setup' => $this->_moduleDataSetup
]);
$eavSetup->addAttributeSet(self::ENTITY_TYPE, $name, 0);
}
}
但是该集没有分配给它的属性。我如何根据默认设置创建集合,以便它预先填充基本属性?
非常感谢任何帮助。
事实证明,AttributeSetManagementInterface 模型有一个创建函数,它采用一个可选的骨架 ID,新集合可以基于该 ID。我最终使用 objectManager 进行快速修复,我确信有更好的方法。
下面对上面的 OP 代码进行扩展:
public function createSet($name)
{
$eavSetup = $this->_eavSetupFactory->create([
'setup' => $this->_moduleDataSetup
]);
$defaultId = $eavSetup->getDefaultAttributeSetId(self::ENTITY_TYPE);
$model = $this->_objectManager
->create('Magento\Eav\Api\Data\AttributeSetInterface')
->setId(null)
->setEntityTypeId(4)
->setAttributeSetName($name);
$this->_objectManager
->create('Magento\Eav\Api\AttributeSetManagementInterface')
->create(self::ENTITY_TYPE, $model, $defaultId)
->save();
}