以编程方式创建属性集和属性

时间:2012-04-13 20:25:21

标签: magento attributes

我正在使用以下链接中列出的代码:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/programmatically_adding_attributes_and_attribute_sets

一切正常,直到这一点:

    // Just add a default group.
    else
    {
        $this->logInfo("Creating default group [{$this->groupName}] for set.");

        $modelGroup = Mage::getModel('eav/entity_attribute_group');
        $modelGroup->setAttributeGroupName($this->groupName);
        $modelGroup->setAttributeSetId($id);

        // This is optional, and just a sorting index in the case of
        // multiple groups.
        // $modelGroup->setSortOrder(1);

        $model->setGroups(array($modelGroup));
    }

我不确定需要在哪里设置对象引用 - 我试图将其作为一个可以自动化的单独文件 - 我正在运行此文件

require_once 'app/Mage.php'; 
Mage::app(); 

非常感谢任何帮助

3 个答案:

答案 0 :(得分:12)

您需要在模块config.xml中有一个看起来像这样的块

<resources>
    <namespace_module_setup><!-- becomes folder under sql dir -->
        <setup>
            <module>Namespace_Module</module>
            <class>Mage_Eav_Model_Entity_Setup</class>
        </setup>
    </namespace_module_setup>
</resources> 

这将允许您将安装程序代码放在XML中的目录中。您需要确保安装程序文件中列出的版本与模块的<version>1.2.0</version>匹配,否则Magento将无法运行安装程序。要添加属性Set,您可以使用以下数据,我从未使用过它,但entityTypeId分别定义了客户,运输,类别,产品实体,分别为1,2,3,4。

/**
     * Add Attribute Set
     *
     * @param mixed $entityTypeId
     * @param string $name
     * @param int $sortOrder
     * @return Mage_Eav_Model_Entity_Setup
     */
    public function addAttributeSet($entityTypeId, $name, $sortOrder = null)
    {
        $data = array(
            'entity_type_id'        => $this->getEntityTypeId($entityTypeId),
            'attribute_set_name'    => $name,
            'sort_order'            => $this->getAttributeSetSortOrder($entityTypeId, $sortOrder),
        );

        $setId = $this->getAttributeSet($entityTypeId, $name, 'attribute_set_id');
        if ($setId) {
            $this->updateAttributeSet($entityTypeId, $setId, $data);
        } else {
            $this->_conn->insert($this->getTable('eav/attribute_set'), $data);

            $this->addAttributeGroup($entityTypeId, $name, $this->_generalGroupName);
        }

        return $this;
    }

这是将属性添加到集合的代码,只需更改属性集数据

//app/code/local/Namespace/Module/sql/Namespace_Module_setup/mysql4-install-1.0.0.php
    $installer = $this;
    /* @var $installer Mage_Eav_Model_Entity_Setup */

    $installer->startSetup();

        $data= array (
            'attribute_set' =>  'Default',
            'group' => 'General',
            'label'    => 'Some Label',
            'visible'     => true,
            'type'     => 'varchar', // multiselect uses comma-sep storage
            'input'    => 'text',
            'system'   => true,
            'required' => false,
            'user_defined' => 1, //defaults to false; if true, define a group
        );

        $installer->addAttribute('catalog_product','attriute_code',$data)

        $installer->endSetup();

以上是模块的属性安装的工作示例。

答案 1 :(得分:5)

有一个更简单的单线程。只需为您的安装脚本扩展Mage_Eav_Model_Entity_Setup,并在安装程序中使用以下内容:

$installer = $this;
/* @var $installer Mage_Eav_Model_Entity_Setup */

$installer->startSetup();
$installer->addAttributeSet(Mage_Catalog_Model_Product::ENTITY, 'New Attribute Set Name');
$installer->endSetup();

答案 2 :(得分:1)

您应该将其作为安装程序实现,因此Magento会在安装(或升级)模块时加载它。在示例中,$this表示安装程序类。

您可以使用https://bitbucket.org/alexsiri7/qbmagemoduleshell创建模块和安装程序,然后在那里添加该代码。该工具是我开发的模块创建者。

相关问题