3

Lots of confusion on this point, hopefully you will understand my areas of lacking understanding by the trend of my questioning:

  1. How does one find available methods for objects in Magento and the general object architecture?

  2. Is there a way to get NetBeans code completion to help with Magento methods? Any helpful software for this?

  3. Where to look for methods first? Models, helpers, blocks?

I realize that I can go one by one through the files in the inheritance tree, but even then I never seem to find the right methods, or the order in which to call them.

Examples of calls I wouldn't have known were possible:

  • Generating a product list:
    {{block type="catalog/product_list" category_id="YOUR_CATEGORY_ID" template="catalog/product/list_tabs.phtml"}}

--I understand after reading the _getProductCollection() method (Mage/Catalog/Block/Product/List.php, line 78 for relevant part) that simply providing list_tabs.phtml with a category_id would generate a list of products in a category, but how do I find stuff like this out without reading so much code!

And how would I have known that "category_id" would be found by load($this->getCategoryId()) . What's this "load"?

  • Something that I'd like to do: Put a single product on the home page using Mage_Catalog_Block_Product_Widget_Link, but title it with the name of the category.

I.e., how would I find the method for getting the product category from
$this->_entityResource = Mage::getResourceSingleton('catalog/product');
(in Mage/Catalog/Block/Product/Widget/Link.php)

Thanks!!!!

2
  1. All classes derive from an Abstract class, in which you should get the basic methods. There is no definitive way (to my knowledge) to find which methods are available to you given one object.
    Just know that, extending Zend, Magento allows you to use magical methods to get/set an object's members. Eg:

    Mage::getModel('catalog/product')->load(1234)->getMyCustomAttribute();

  2. Netbeans doesn't seem to help a lot, you can get code completion just like in any other IDE for things that have previously been defined. You'll be able to override methods for classes that extend other classes (useful when you override Models/controllers).
    But you'll mostly be able to get abstract methods (or Zend methods) when you Ctrl+space.
    What's useful is going to method declaration to find out how Magento defines things. But given the level of abstraction in Magento, it can sometimes get confusing.

  3. It's a tricky question, as it depends on what you are looking for. For example, if you are in a template (.phtml) file, you should look for methods in Blocks (eg: $this->getLoadedProductCollection() in product templates, relates to the loading of the collection of products done in the Block).
    Actions are defined in controllers, and accesses to data are defined in the Models (following the MVC pattern).

->load(ID) is a method available in all models to load an entry from the database. eg:

Mage::getModel('catalog/product')->load(1234); //will load product 1324
Mage::getModel('catalog/category')->load(321); //will load category 321
Mage::getModel('customer/customer')->load(555); //will load customer 555 ... and so on  

To put a single product on the home page is a piece of cake, all you have to do is define a template file as your home page and Mage::getModel('catalog/product')->load(1234); in that .phtml file. You can even do

Mage::getModel('catalog/product')->load(Mage::getModel('core/variable')->loadByCode('your_magento_variable')->getValue('html'));  

Allowing you to define a custom variable in which you'd store your product id (for the Magento administrator to change it as they will in System > Custom variables

  • most thorough and helpful answer ever. you rock! – blisstdev May 15 '14 at 17:08
  • glad it helped :) – Julien Lachal May 16 '14 at 12:37
2

Another method which has really helped me finding what is going on with objects is:

Zend_Debug::dump($obj->getData())

  • what's the difference between this and a simple var_dump? I've never give it a try so I'd like to know. – Julien Lachal Jun 11 '14 at 15:19
  • I just always get information out of it. For example: var_dump($menuTree) yields nothing. Zend_Debug::dump($menuTree) yields: <pre>array(8) { [&quot;name&quot;] =&gt; string(5) &quot;Socks&quot; [&quot;id&quot;] =&gt; string(15) &quot;category-node-4&quot; [&quot;url&quot;] =&gt; string(41) &quot;http://&quot; [&quot;is_active&quot;] =&gt; bool(false) [&quot;level&quot;] =&gt; int(0) [&quot;is_first&quot;] =&gt; bool(true) [&quot;is_last&quot;] =&gt; bool(false) [&quot;position_class&quot;] =&gt; string(5) &quot;nav-1&quot; } </pre> – blisstdev Jun 14 '14 at 22:22

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.