2012-03-19 129 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

相关问题