【问题标题】:create attribute set in Magento setup script在 Magento 设置脚本中创建属性集
【发布时间】:2012-06-19 06:20:37
【问题描述】:

创建属性并将它们分配给现有属性集是一个已解决的问题,但我们遇到了一个问题,尝试创建属性集并使用默认属性和特定属性填充它失败了。这是正在使用的代码:

$setup->addAttributeSet('catalog_product', 'women_sizing_denim');

$oAttributeSetModel = Mage::getModel("eav/entity_attribute_set")
        ->load($setup->getAttributeSetId('catalog_product', 'women_sizing_denim'))
        ->initFromSkeleton($setup->getAttributeSetId('catalog_product', 'default'))
        ->save();

我可以通过调试验证initfromSkeleton 方法确实从广告中的默认attribute_set 加载属性,但是在save() 之后,新集合为空。

向集合添加新属性是可能的,因此它确实存在并且被正确创建,但是缺少默认属性使其无法使用,因为 SKU、价格、名称等都是必需的。

【问题讨论】:

    标签: php magento attributes entity-attribute-value


    【解决方案1】:

    我记得基于默认属性集创建属性集的问题是,您需要保存属性集两次,一次在调用initSkeleton() 之前,一次在调用之后。 p>

    我不记得确切的原因了,那是很久以前的事了。无论如何,这对我有用:

    // Mage_Eav_Model_Entity_Setup
    $oEntitySetup = $this;
    $oEntitySetup->startSetup();
    
    $sNewSetName = 'myset';
    $iCatalogProductEntityTypeId = (int) $oEntitySetup->getEntityTypeId('catalog_product');
    
    $oAttributeset = Mage::getModel('eav/entity_attribute_set')
        ->setEntityTypeId($iCatalogProductEntityTypeId)
        ->setAttributeSetName($sNewSetName);
    
    if ($oAttributeset->validate()) {
        $oAttributeset
            ->save()
            ->initFromSkeleton($iCatalogProductEntityTypeId)
            ->save();
    }
    else {
        die('Attributeset with name ' . $sNewSetName . ' already exists.');
    }
    
    $oEntitySetup->endSetup();
    

    【讨论】:

    • 感谢 Jurgen,将尝试一下。我见过其他需要在修改之前保存 Magento 对象的实例,这不合逻辑,但绝对有可能!
    • 保存两次对我来说是真正的问题。感谢您的回答!
    【解决方案2】:

    请注意安装类需要扩展

    Mage_Catalog_Model_Resource_Eav_Mysql4_Setup
    

    这样

    $oEntitySetup->getEntityTypeId('catalog_product');
    

    可以调用。

    【讨论】:

      【解决方案3】:

      我使用了 Jürgen Thelen 的答案,它很有效。

      但我发现新的属性集没有默认选项和选项组,例如一般和发票等。

      所以要解决这个问题,在 initFromSkeleton() 中包含 $installer->getAttributeSetId('catalog_product', 'default')

      if($attributeSet->validate()) {
      $attributeSet
          ->save()
          ->initFromSkeleton($installer->getAttributeSetId('catalog_product', 'default'))
          ->save();
      } else {
      die('Attributeset with name ' . $setName . ' already exists.');
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-07
        相关资源
        最近更新 更多