4

I have added a custom category attribute to my Magento installation with the following code:

<?php
//  initialize magento environment for 'default' store
    require_once 'app/Mage.php';
    Mage::app('default');

    echo "initialising";
    echo runAdd();
    echo "finishing";

function runAdd()
{
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    // below code will add text attribute
    $setup->addAttribute('catalog_category', 'test_field', array(
        'group'         => 'General',
        'input'         => 'text',
        'type'          => 'varchar',
        'label'         => 'a new text field',
        'backend'       => '',
        'visible'       => 1,
        'required'      => 0,
        'user_defined'  => 1,
        'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    ));
}

I am having great difficulty actually retrieving the value I have set in the back end. I have tried every variation of:

$_category = Mage::getModel('catalog/category')->load(1000);               
$data = $_category->getData();        
$atts= $_category->getAttributes();
var_dump($data );
var_dump($atts);

etc but am stuck.

Does anyone know how I'd retrieve this data?

Gga
  • 4,311
  • 14
  • 39
  • 74
  • Did you check if the attribute was actually added? – FlorinelChis Oct 25 '12 at 12:22
  • @FlorinelChis It appears in the backend admin and remains after saving and changing categories so I assume it has been added correctly. Is their a particular method of verifying that you'd know of beyond that? – Gga Oct 25 '12 at 12:25
  • After saving that category with the ID 1000 and reindexing the problem remained the same? – FlorinelChis Oct 25 '12 at 12:32
  • @Rodgers and Hammertim may be you forgot to given frontend visible "true" during attribute creation in backend. – Ravichandran Jothi Oct 25 '12 at 12:36
  • @FlorinelChis if you mean flushing the cache by reindexing, then yes no difference unfortunately. – Gga Oct 25 '12 at 12:48
  • @Ravichandran what attribute would that be? All the attributes I set are in the above example, is it not in there? – Gga Oct 25 '12 at 12:49

2 Answers2

4

You shouldn't update DB in standalone scripts but you must use upgrade scripts ...

If you had, you wouldn't had to instantiate manually the installer and wouldn't have instantiate a bad one : you have to use Mage_Catalog_Model_Resource_Setup (or Mage_Catalog_Model_Resource_Eav_Mysql4_Setup depending on your magento version) because Mage_Catalog uses a custom Setup class configured in the config.xml :

<resources>
    <catalog_setup>
        <setup>
            <module>Mage_Catalog</module>
            <class>Mage_Catalog_Model_Resource_Setup</class>
        </setup>
    </catalog_setup>
</resources>

I suggest you to adapt this in your module config.xml and use a install/upgrade script to modify your DB properly.

Jscti
  • 14,096
  • 4
  • 62
  • 87
4

you can write folloing line may be help you

$_category = Mage::getModel('catalog/category')->load(1000)->getData('test_field');
Tim Post
  • 33,371
  • 15
  • 110
  • 174
Vishal
  • 41
  • 1