简单地说,在添加新的编辑选项卡后,我在 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
我的代码中有这个:
<?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 函数。我是否忽略了一些导致我无法调用此函数的东西?
总结.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>
最佳答案
您没有正确获取布局对象 (Mage_Core_Model_Layout
)。在 Action Controller 和 block 中,它是 $this->getLayout()->createBlock()
,在其他任何地方都是 Mage::app()->getLayout()->createBlock()
编辑:Sergy 还指出未加载布局对象,这让我意识到您使用的是 php __construct()
,而不是典型的 Magento _construct()
。 block 实例在 Mage_Core_Model_Layout::createBlock()
中被实例化(并且它们的构造函数被调用)之后才在其上设置布局对象 - 注意该方法中 block 实例如何获取布局通过其 setLayout()
方法对其进行设置。这就是 block 方法 _prepareLayout()
背后的目的 - 它是一种类似于构造函数的方法,在创建 block 实例后触发。
对以下代码的更正:
<?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());
}
// ...
}
关于Magento - 在非对象上调用成员函数 createBlock(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9779149/