Programatically get max attribute_id and max attributeoption_id via API (without SQL Query)

Please LogIn to Reply!

Post By: admin | 26-01-2012 06:35 Reply

already found something about this task but these snippets where just SQL Querys, is it possible to get the highest attribute id / highest attributeoption id

like, when i create an attribute, its got the attribute_id 11, now i use that tool without having created any attribute with it yet --> just read all available attributes, not write, how do i get the value "11" for example with the Magento API without query'ing the mysql database?

to be more specific: what i want to do is to iterate though all existing attributes getting all attribute_ids, attribute_names for output

same for all attribute options of one attribute

didn't found anything usable, maybe I've been looking at the wrong place on the Internet. Hope someone could help me.

Attributes in Magento are represented by the model 'eav/entity_attribute'. The following code shows how to access them. Hope it's what you were looking for.

/*
 * Initialize Magento.  This assumes that we're running the script from the root
 * directory of a magento installation.  Obviously if you're writing an extension
 * you don't need this.
 */
require_once 'app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::getSingleton("core/session")->setUser(Mage::getModel("admin/user")->load(0));
umask(0);

/*
 * Attributes are represented by the eav/entity_attribute model
 */
$model = Mage::getModel("eav/entity_attribute");
foreach($model->getCollection() as $attr){
    //$attr is a Mage_Eav_Model_Entity_Attribute (see Mage_Eav_Model_Entity_Attribute_Abstract for
    //most of the action).

    //Replace with your attribute processing code
    echo "id={$attr->getId()}, code={$attr->getAttributeCode()},label={$attr->getFrontendLabel()}n";

    //Check to see if the attribute has options
    if($attr->usesSource()){
        echo "   attribute options";

        //process the options.
        foreach($attr->getSource()->getAllOptions() as $option){
            //Replace with your option processing code.

            $label = $option['label'];
            $value = $option['value'];

            //Normally label is a string and value is an int.  However,
            //some options have sub options.  In this case value is like
            //array( array('label'=>'Label', 'value'=>'...'), ...)
            if(is_array($value)){
                $value = print_r($value, true);
            }
            echo "    $label => $valuen";
        }
    }
}

Some other neat things you can do with attributes:

//Load an attribute by code/name
Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'price');
//Get the attribute set for an attribute
$setId = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'price')->getAttributeSetId();
$set   = Mage::getModel('eav/entity_attribute_set')->load($setId);

See app/code/core/Mage/Eav/Model/Entity/Attribute.php, app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php

`Reply Or Comment!
Please LogIn to Reply!