Magento - 在非对象上调用成员函数createBlock()

Red*_*dox 2 magento

简而言之,我在添加新的编辑选项卡后,在Magento的产品管理中收到此错误.

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
Run Code Online (Sandbox Code Playgroud)

我在我的代码中有这个:

<?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');
    }
}
Run Code Online (Sandbox Code Playgroud)

以前我只能调用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>
Run Code Online (Sandbox Code Playgroud)

ben*_*rks 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());
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)