Magento模块的安装问题 - 向类别添加属性

时间:2011-11-02 17:21:12

标签: php magento module

今天下午,我一直在尝试在Magento中创建一个模块,只需在类别中添加更多属性。

我将浏览到目前为止创建的脚本......

应用程序的/ etc /模块/ Comp_Categoryattributes.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Comp_Categoryattributes>
            <active>true</active>
            <codePool>local</codePool>
        </Comp_Categoryattributes>
    </modules>
</config>

应用程序/代码/本地/ COMP / Categoryattributes的/ etc / config.xml中

<?xml version="1.0"?>
<config>
    <modules>
        <Comp_Categoryattributes>
            <version>0.1.0</version>
        </Comp_Categoryattributes>
    </modules>
    <global>
        <models>
            <categoryattributes>
                <class>Comp_Categoryattributes_Model</class>
                <resourceModel>categoryattributes_mysql4</resourceModel>
            </categoryattributes>
            <categoryattributes_mysql4>
                <class>Comp_Categoryattributes_Model_mysql4</class>
            </categoryattributes_mysql4>
        </models>
        <resources>
            <categoryattributes_setup>
                <setup>
                    <module>Comp_Categoryattributes</module>
                    <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </categoryattributes_setup>
            <ncategoryattributes_setup_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </categoryattributes_setup_write>
            <categoryattributes_setup_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </categoryattributes_setup_read>
        </resources>
    </global>
</config>

然后最后,这个礼貌http://inchoo.net/ecommerce/magento/how-to-add-new-custom-category-attribute-in-magento/ 应用程序/代码/本地/ COMP / Categoryattributes / SQL / categoryattributes_setup / mysql4安装-0.1.0.php

<?php

$installer = $this;
$installer->startSetup();

$entityTypeId     = $installer->getEntityTypeId('catalog_category');
$attributeSetId   = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute('catalog_category', 'twitter_user',  array(
    'type'     => 'int',
    'label'    => 'Twitter Username',
    'input'    => 'text',
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'default'           => 0
));

$installer->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'twitter_user',
    '11'                    //last Magento's attribute position in General tab is 10
);

$attributeId = $installer->getAttributeId($entityTypeId, 'twitter_user');

$installer->run("
INSERT INTO `{$installer->getTable('catalog_category_entity_int')}`
(`entity_type_id`, `attribute_id`, `entity_id`, `value`)
    SELECT '{$entityTypeId}', '{$attributeId}', `entity_id`, '1'
        FROM `{$installer->getTable('catalog_category_entity')}`;
");

//this will set data of your custom attribute for root category
Mage::getModel('catalog/category')
    ->load(1)
    ->setImportedCatId(0)
    ->setInitialSetupFlag(true)
    ->save();

//this will set data of your custom attribute for default category
Mage::getModel('catalog/category')
    ->load(2)
    ->setImportedCatId(0)
    ->setInitialSetupFlag(true)
    ->save();

$installer->endSetup();

?>

该模块出现在后端的禁用模块输出区域中,但我没有看到在core_resource表中出现categoryattribute_setup?有什么明显的东西我不见了吗?

干杯,

汤姆

1 个答案:

答案 0 :(得分:1)

只是略过它,我注意到你的config / xml文件包含以下内容:

<ncategoryattributes_setup_write>

尝试删除'n'。