0

I need to add 2 attributes to category.

What I've done so far is in config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <[Namespace]_Category>
            <!-- Make sure that the version number matches the filename on your install script! -->
            <version>0.0.1</version>
        </[Namespace]_Category>
    </modules>
    <global>
        <resources>
            <custom_category>
                <setup>
                    <module>[Namespace]_Category</module>
                    <!-- This next line is absolutely critical so that we call the appropriate setup class -->
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                    <connection>
                        <use>core_setup</use>
                    </connection>
                </setup>
            </custom_category>
            <custom_category_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </custom_category_write>
            <custom_category_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </custom_category_read>
        </resources>
    </global>
</config>

And the corresponding mysql4-install-0.0.1.php:

<?php
$installer = $this;
$installer->startSetup();
/**
 * This is an entity associated with catalog_category
 * @var  integer
 */
$entityTypeId     = $installer->getEntityTypeId('catalog_category');
/**
 * Use the default attribute set for catalog_category -- this refers to the table `eav_attribute_set`
 * In my case, it's 3, but this function should automatically get that for you.
 * @var integer
 */
$attributeSetId   = $installer->getDefaultAttributeSetId($entityTypeId);
/**
 * This determines what group (tab) that the field will be placed. "General Information"
 * is the default tab, and I'm okay with this, but see `eav_attribute_group` for other
 * groups
 * @var integer
 */
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
/**
 * Let's set up our attribute, customize the type as needed (e.g. int, varchar, decimal)
 * Note where I've used `internal_title` and change to your variable.
 */
$installer->addAttribute('catalog_category', 'social_image',  array(
    'type'     => 'varchar', /* Type - see eav_entity_* for the different types */
    'label'    => 'Social-Image', /* Your label */
    'input'    => 'image', /* This refers to the type of form field should display*/
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => TRUE,
    'required'          => FALSE,
    'user_defined'      => FALSE,
    'default'           => ''
));
$installer->addAttribute('catalog_category', 'cannonical_url',  array(
    'type'     => 'text', /* Type - see eav_entity_* for the different types */
    'label'    => 'Cannonical Url', /* Your label */
    'input'    => 'text', /* This refers to the type of form field should display*/
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => TRUE,
    'required'          => FALSE,
    'user_defined'      => FALSE,
    'default'           => ''
));
/**
 * Now, add the attribute to the proper attribute group - again, replace the variable title
 * with yours.
 */
$installer->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'social_image',
    '2' /* Refers to the sort order of fields - see `eav_entity_attribute` for reference on the location of other fields.  I want this right below the active field, so 2 works for me.*/
);
$installer->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'cannonical_url',
    '2' /* Refers to the sort order of fields - see `eav_entity_attribute` for reference on the location of other fields.  I want this right below the active field, so 2 works for me.*/
);
$installer->endSetup();

When I tried this for the first time it worked and made the first attribute. When I added the second attribute, nothing more happend.

Is there any logic to use the mysql script only once?

If yes, how do I have to do the update-process?

Is there a way to roll all this back? The fields won't disappear on extension deaktivation.

The first attribute should create a image field.... which it does, but if I try to upload, nothing happens.

Do I have to direct this to the media folder?

(That's where I would want to select)

Thanks for helping me out!

1

You cannot create another new attribute using exiting sql installer.

Magento uses Setup Resource Models to perform install and upgrade operations for modules. These are executed during the application initialization where each Setup Resource is allowed to apply any updates it requires. This is usually done by inspecting the installed version of the module from the core_resource table and executing any setup scripts defined.

Setup resources are defined in config.xml:

<config>
    <global>
        <resources>
            <{resource_name}>
                <setup>
                    <module>{module_name}</module>
                    <class>{setup_resource_class}</class>
                </setup>
            </{resource_name}>
        </resources>
    </global>
</config>

Then the install and upgrade scripts, which are simply PHP scripts executed by including them within the setup resource, are placed in {module_root}/sql/{resource_name}/ for system install/upgrade scripts or {module_root}/data/{resource_name}/ for data install/upgrade scripts.

The scripts use the following naming scheme:

  • install-{version}.php

  • update-{from_version}-{to_version}.php

  • data-install-{version}.php

  • data-upgrade-{from_version}-{to_version}.php

In older versions of Magento, prior to Magento CE 1.6 and Magento EE 1.11, the install/upgrade script name would be prefixed by resource type, e.g. mysql4.

If the module is not present in the database, the Setup Model will install the module by first running the latest install script and then the upgrade scripts since the install script version.

If the module version in config.xml is higher than the version in the database, the Setup Model performs an upgrade by running all upgrade scripts which have a from-version higher or equal to the database venison and to-version lower or equal to the new config version.

So,create another magento sql installer script name of

mysql4-upgrade-0.0.1-0.1.1.php.

And write your installer code. and also change module version to

0.1.1

from

0.0.1

0

The first attribute should create a image field.... which it does, but if I try to upload, nothing happens.

For image upload you need to create backend for that attribute which will handle image upload process

$installer->addAttribute('catalog_category', 'social_image',  array(
    'type'     => 'varchar', 
    'label'    => 'Social-Image', 
    'input'    => 'image', 
    'backend'  => 'Namespace/category_attribute_backend_image',
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => TRUE,
    'required'          => FALSE,
    'user_defined'      => TRUE,   
));

create backend model

class Namespace_Category_Model_Attribute_Backend_Image extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{

    /**
     * Save uploaded file and set its name to category
     *
     * @param Varien_Object $object
     */
    public function afterSave($object)
    {
        $value = $object->getData($this->getAttribute()->getName());

        if (is_array($value) && !empty($value['delete'])) {
            $object->setData($this->getAttribute()->getName(), '');
            $this->getAttribute()->getEntity()
                ->saveAttribute($object, $this->getAttribute()->getName());
            return;
        }

        $path = Mage::getBaseDir('media') . DS . 'Namespace' . DS . 'category' . DS;

        try {
            $uploader = new Mage_Core_Model_File_Uploader($this->getAttribute()->getName());
            $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
            $uploader->setAllowRenameFiles(true);
            $result = $uploader->save($path);

            $object->setData($this->getAttribute()->getName(), $result['file']);
            $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
        } catch (Exception $e) {
            if ($e->getCode() != Mage_Core_Model_File_Uploader::TMP_NAME_EMPTY) {
                Mage::logException($e);
            }
            /** @TODO ??? */
            return;
        }
    }
}

increment your setup version as you have already created attribute with name social_image

  • @Bernhard Prange is above code working ? – Minesh Patel Oct 8 '15 at 18:28

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.