创建属性设置和编程属性 [英] Creating attribute sets and attributes programmatically

查看:91
本文介绍了创建属性设置和编程属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的链接中列出的code:

I am using the code listed on the following link:

<一个href=\"http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/programmatically_adding_attributes_and_attribute_sets\" rel=\"nofollow\">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));
    }

我不能确定在对象引用将需要从设置 - 我试图有此作为可以自动一个单独的文件 - 我通过执行运行该文件中的

I am unsure where the object reference would need to be set from - I am attempting to have this as a separate file that can be automated - I am running this file by doing a

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

在此任何帮助将大大AP preciated

Any help in this would be greatly appreciated

推荐答案

您将需要在你的模块的config.xml块,看起来像这样

You will need to have a block in your modules config.xml that looks like so

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

这将允许您将您的安装程序code。在你的XML目录。您需要确保在安装程序文件中列出的版本相匹配的&LT;版本&GT; 1.2.0&LT; /版本&GT; 你的模块,否则的Magento将无法运行您的安装程序。要添加一个属性集,可以使用下面的数据,我从来没有使用它,但entityTypeId确定它是否是一个客户,航运,类别,产品实体,每1,2,3,4分别。

This will allow you to place your installer code in the directory that is in your XML. You need to make sure that the version listed on the installer file matches the <version>1.2.0</version> of your module otherwise Magento will not be able to run your installer. To add an attribute Set, you can use the following data, I have never use it, but the entityTypeId defines whether it is a customer, shipping, category, product entity, each 1, 2, 3, 4 respectively.

/**
     * 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;
    }

这是code添加属性为一组,只需更改属性设置数据

This is the code for adding an attribute to a set, just change the attribute set data

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

以上是一个属性用于安装模块的工作示例。

The above is a working example of an attribute installation for a module.

这篇关于创建属性设置和编程属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆