Search code examples
magentophpdoc

Having trouble navigating Magento documentation


I am brand new to Magento and the documentation, primarily the phpDocs, are difficult to navigate. For example,

$attributeSet = Mage::getModel('eav/entity_attribute_set')->load($id);

In the php doc for Class Mage_Eav_Model_Entity_Attribute_Set there is no mention of the method getAttributeSetName() either in inherited methods or otherwise and yet this works.

$attributeSet = Mage::getModel('eav/entity_attribute_set')->load($id);
echo $attributeSet->getAttributeSetName();

So I suppose I have several questions.

  1. Can someone explain to me why the documentation is this way?
  2. Where I can find the mysterious getAttributeSetName() method in the phpDocs?

My theory is that there is some inheritance or a design pattern implementation going on that I'm not understanding, maybe someone can shed some light on this for me.


Solution

  • If you really want to fry your brain, take a look at the source code for Mage_Eav_Model_Entity_Attribute_Set and follow the inheritance chain all the way back. You won't find a getAttributeSetName method defined anywhere.

    All Magento objects that inherit from Varien_Object can have arbitrary data members set on them. Try this.

    $attributeSet = Mage::getModel('eav/entity_attribute_set')->load($id);
    $attributeSet->setFooBazBar('Value');
    var_dump($attributeSet->getFooBazBar());
    var_dump($attributeSet->getData('foo_baz_bar'));
    var_dump($attributeSet->setData('foo_baz_bar','New Value'));
    var_dump($attributeSet->getFooBazBar());
    

    You can also get all the data members by using

    var_dump($attributeSet->getData());
    

    but be careful dumping these, because if there's a data object that has a circular reference and you're not using something like xDebug, then PHP will have a fit trying to display the object.

    Magento stores data properties in a special _data array property. You can get/set values in this array with getData and setData. Magento also has implemented magic getting and setter methods, so when you say something like

    $object->getFooBazBar();
    

    The method getFooBazBar is transformed into the data property foo_baz_bar. and then getData is called using this property. It's a little tricky to get your head around, but once you get it you'll start to see how much time you can save using this pattern.

    One side effect of this is, of course, it's impossible to infer what data properties any object might have by looking at it's class file, so there's no phpDocs for these methods.