2012-03-19 53 views
2

簡單地說,在添加新的編輯選項卡後,我在Magento的產品管理中收到此錯誤。Magento - 在非對象上調用成員函數createBlock()

Fatal error: Call to a member function createBlock() on a non-object in 
/var/www/app/code/local/RedoxStudios/ErpTab/Block/Adminhtml/Catalog/Product/Tab.php 
on line 11 

我在我的代碼有這樣的:

<?php 
class RedoxStudios_ErpTab_Block_Adminhtml_Catalog_Product_Tab 
extends Mage_Adminhtml_Block_Template 
implements Mage_Adminhtml_Block_Widget_Tab_Interface { 

    /* 
    * Set the template for the block 
    */ 
    public function __construct() { 
     parent::__construct(); 
     $this->getLayout()->createBlock('Purchase/Product_Widget_StockDetails_Summary'); 
     $this->setProduct($this->getProduct()); 
     $this->setTemplate('Purchase/Product/StockDetails/Summary.phtml'); 
    } 

    /** 
    * Return current product instance 
    * 
    * @return Mage_Catalog_Model_Product 
    */ 

    public function getProduct() 
    { 
     return Mage::registry('product'); 
    } 
} 

以前我是能夠只需調用createBlock功能。我忽略了一些讓我無法調用這個函數的東西?


Summary.phtml:

<div class="stock-details-summary"> 

<table border="0"> 
    <tr> 
     <td class="a-right"><?php echo $this->__('Waiting for delivery'); ?> : </td> 
     <td class="a-right"><?php echo ($this->getWaitingForDeliveryQty() ? $this->getWaitingForDeliveryQty() : 0); ?></td> 
    </tr> 
    <tr> 
     <td class="a-right"> 
      <?php echo $this->__('Manual supply need'); ?> : 
      <?php if ($this->getManualSupplyNeedQty() > 0): ?> 
       <i><?php echo $this->getProduct()->getmanual_supply_need_comments(); ?></i> 
      <?php endif; ?> 
     </td> 
     <td class="a-right"> 
      <?php echo $this->getManualSupplyNeedQty(); ?> 
     </td> 
    </tr> 
    <tr> 
     <td class="a-right"><?php echo $this->__('Min qty to purchase'); ?> : </td> 
     <td class="a-right"><font color="red"><?php echo $this->getTotalNeededQtyForValidOrdersMinusWaitingForDelivery(); ?></font></td> 
    </tr> 
    <tr> 
     <td class="a-right"><?php echo $this->__('Max qty to purchase'); ?> : </td> 
     <td class="a-right" width="60"><font color="red"><?php echo $this->getTotalNeededQtyMinusWaitingForDelivery(); ?></font></td> 
    </tr> 
    <tr> 
     <td class="a-right"><?php echo $this->__('Status'); ?> : </td> 
     <td class="a-right"><?php echo $this->getGeneralStatus(); ?></td> 
    </tr> 
</table> 

</div> 

回答

10

你沒有得到正確的佈局對象(Mage_Core_Model_Layout)。在動作控制器,並阻止它的$this->getLayout()->createBlock(),其他地方是Mage::app()->getLayout()->createBlock()

編輯:Sergy還指出,佈局對象不加載,這使我意識到你正在使用PHP __construct(),而不是典型的Magento _construct()。塊實例沒有設置佈局對象,直到它們在Mage_Core_Model_Layout::createBlock()中被實例化(以及它們的構造函數被調用)之後 - 請注意該方法塊實例通過其setLayout()方法獲取其佈局設置的方式。這是塊方法_prepareLayout()背後的目的 - 它是一個類似於構造函數的方法,在塊實例創建後觸發。

更正如下代碼:

<?php 
class RedoxStudios_ErpTab_Block_Adminhtml_Catalog_Product_Tab 
extends Mage_Adminhtml_Block_Template 
implements Mage_Adminhtml_Block_Widget_Tab_Interface { 

    /* 
    * Set the template for the block 
    */ 
    protected function _construct() 
    { 
     $this->setTemplate('Purchase/Product/StockDetails/Summary.phtml'); 
    } 

    public function _prepareLayout() 
    { 
     $this->getLayout()->createBlock('Purchase/Product_Widget_StockDetails_Summary'); 
     $this->setProduct($this->getProduct()); 
    } 

    // ... 
} 
+0

但他確實在方框類,不是嗎? – Zyava 2012-03-20 07:21:17

+1

看起來像這個塊沒有設置佈局。 Mage :: app() - > getLayout()將返回爲當前句柄加載的佈局。 – Sergey 2012-03-20 07:54:15

+0

@Sergy部分正確。佈局尚未設置(更新上面的答案),但'Mage :: app() - > getLayout()'只是一個對象實例來管理塊;在Mage_Core_Model_Layout_Update的幫助下,句柄可能已經或可能不會被用於添加塊。 – benmarks 2012-03-20 11:37:10

相關問題