3

我目前有以下代码创建一个新的属性集:

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);

    }

}

但是,该集合没有分配给它的属性。我将如何根据默认设置创建集合,以便预先填充基本属性?

非常感谢任何帮助。

4

1 回答 1

2

事实证明 AttributeSetManagementInterface 模型有一个 create 函数,该函数接受一个可选的骨架 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();

}
于 2016-01-27T14:14:22.607 回答