开发者

magento, help to understand default product Template code

I am a novice in magento but am trying to learn it in the least possible time. I Was going through: http://alanstorm.com/layouts_blocks_and_templates which says:

File: app/design/frontend/base/default/template/catalog/product/list.phtml contains :

<?php $_productCollection=$this->getLoadedProductCollection() ?>    <?php if(!$_productCollection->count()): ?> <div class="note-msg">
    <?php echo $this->__("There are no products matching the selection.") ?>    </div>
<?php else: ?>  

The getLoadedProductCollection method can be found in the Template’s Block, Mage_Catalog_Block_Product_List ... and from there:

File: app/code/core/Mage/Catalog/Block/Product/List.php

...
public function getLoadedProductCollection()
{
    return $this->_getProductCollection();
}   
...

After that , the aforementioned page writes : The block’s _getProductCollection then instantiates models and reads their data, returning a result to the template.

I am just at a loss here. _getProductCollection() has this line:

 if (is_null($this->_productCollection))

1) Does _productCollection mean the protected variable $_productCollection ?

 if (is_null($this->_productCollection)) {
            $layer = $this->getLayer();

2) What is the explanation of $layer = $this->getLayer() plz?

After that I get:

 if ($this->getShowRootCategory()) {
                $this->setCategoryId(Mage::app开发者_StackOverflow中文版()->getStore()->getRootCategoryId());
            }

3) Where is the method getShowRootCategory() ?

4)What approach can help me understand the pros and cons of the line :

$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());

5) My questions may sound so easy to many. But can you refer to any online resource to learn all these things and others as a beginner of magento?

Good Luck


This if (is_null($this->_productCollection)) is a caching technique that is common to OOP code as methods cache their variables in object context and if the methods are called multiple times against same object or within the same object then it delivers the cached variable instead of asking it all over from database again.

you can answer your other questions by grepping the codebase or finding the methods you are curious about from source code. Sometimes a method is a magic method (set, get) and you won't find a method definition behind that

grep ' getRootCategoryId' app/code/ -rsn
app/code/core/Mage/Core/Model/Store/Group.php:275:    public function getRootCategoryId()
app/code/core/Mage/Core/Model/Store.php:850:    public function getRootCategoryId()

and the very basic grep patterns to use are:

  • use [space]methodname( to find the methods and where they are defined in codebase
  • use >methodname( to find where this method is called in codebase
  • use >setMethodName( to find where magic methods are set if you wonder why there is no definition for getSomeVariable() in your codebase


1) Yes.

2) Mage_Catalog_Block_Product_List::getLayer() returns catalog layer model (Mage_Catalog_Model_Layer). It is used in the code below.

3) It is magic method, almost all magento classes extend Varien_Object class. Read more about Varien_Object in this article.

4) Sorry, don't understand what this question about.

5) To avoid such questions you should read official development guide first and only after understanding it you should read Alan Storm's articles (which are slightly outdated, btw).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜