致命错误:在非对象上调用成员函数toHtml()

时间:2014-08-28 14:36:19

标签: php magento magento-1.7 magento-1.8

我有定制模块珠宝。管理表单中使用的选项卡在类别编辑中。下面的代码用于在管理表单中添加选项卡。它会给出致命的错误

Fatal error: Call to a member function toHtml() on a non-object in D:\wamp\www\avita\app\code\local\Mage\Adminhtml\Block\Catalog\Category\Tabs.php on line 158

以下是我的文件

1.) local/Mage/Adminhtml/Block/Catalog/Category/Tabs.php

$this->addTab('upload_prices', array(
'label'     => Mage::helper('catalog')->__('Upload prices'),
'content'   => $this->getLayout()->createBlock('jewellery/adminhtml_catalog_category_product_tab')->toHtml(),
));

2.app/code/local/Subora/Jewellery/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Subora_Jewellery>
            <version>0.1.0</version>
        </Subora_Jewellery>
    </modules>
    <global>
        <models>
            <jewellery>
                <class>Subora_Jewellery_Model</class>
            </jewellery>
        </models>
        <helpers>
            <jewellery>
                <class>Subora_Jewellery_Helper</class>
            </jewellery>
        </helpers>
        <blocks>
            <jewellery>
                <class>Subora_Jewellery_Block</class>
            </jewellery>
        </blocks>
        <resources>
            <jewellery_setup>
                <setup>
                    <module>Subora_Jewellery</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </jewellery_setup>
            <jewellery_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </jewellery_write>
            <jewellery_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </jewellery_read>
        </resources>

    </global>
    <adminhtml>
        <layout>
            <updates>
                <jewellery>
                    <file>jewellery.xml</file>
                </jewellery>
            </updates>
        </layout>
        <events>
            <adminhtml_catalog_product_edit_prepare_form>
                <observers>
                    <jewellery_product_edit_prepare_form>
                        <class>jewellery/observer</class>
                        <method>productEditPrepareForm</method>
                    </jewellery_product_edit_prepare_form>
                </observers>
            </adminhtml_catalog_product_edit_prepare_form>
            <catalog_product_save_after>
                <observers>
                    <jewellery_save_product_data>
                        <type>singleton</type>
                        <class>jewellery/observer</class>
                        <method>saveProductTabData</method>
                    </jewellery_save_product_data>
                </observers>
            </catalog_product_save_after>
            <catalog_category_save_after>
                <observers>
                    <jewellery_save_category_data>
                        <type>singleton</type>
                        <class>jewellery/observer</class>
                        <method>saveCategoryTabData</method>
                    </jewellery_save_category_data>
                </observers>
            </catalog_category_save_after>
        </events>

    </adminhtml>

</config> 

3 app \ design \ frontend \ base \ default \ layout \ jewellery.xml

<?xml version="1.0"?>
<layout version="0.1.0">
  <jewellery_adminhtml_jewellerybackend_index>
    <reference name="content">
      <block type="jewellery/adminhtml_jewellerybackend" name="jewellerybackend"/>
    </reference>
  </jewellery_adminhtml_jewellerybackend_index>

    <jewellery_adminhtml_jewellerybackend_save>
        <reference name="content">
            <block type="jewellery/adminhtml_jewellerybackend_save" name="jewellerybackend_save" template="jewellery/jewellerybackend.phtml" />
        </reference>
    </jewellery_adminhtml_jewellerybackend_save>

    <adminhtml_catalog_product_edit>
        <reference name="product_tabs">
            <action method="addTab">
                <name>subora_jewellery_tab</name>
                <block>jewellery/adminhtml_catalog_product_tab</block>
            </action>
        </reference>
    </adminhtml_catalog_product_edit>

    <adminhtml_catalog_product_new>
        <reference name="product_tabs">
            <action method="addTab">
                <name>subora_jewellery_tab</name>
                <block>jewellery/adminhtml_catalog_product_tab</block>
            </action>
        </reference>
    </adminhtml_catalog_product_new>
</layout>

4.local \ Subora \首饰\块\ Adminhtml \目录\类别\产品\ Tab.php

<?php
class Subora_Jewellery_Block_Adminhtml_Catalog_Category_Product_Tab extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm() {
        $form = new Varien_Data_Form();

        $this->setForm($form);

        $fieldset = $form->addFieldset('category_import_prices', array('legend'=>Mage::helper('catalog')->__('Import prices')));
        $fieldset->addField('prices', 'file', array(
            'label' => Mage::helper('catalog')->__('Prices file (CSV only)'),
            'name'=> 'prices',
        ));

        return parent::_prepareForm();
    }
}

现在做什么我试过但不能添加标签??

1 个答案:

答案 0 :(得分:2)

问题仍然存在

$this->getLayout()->createBlock('jewellery/adminhtml_catalog_category_product_tab')->toHtml();

toHtml()以非对象方式调用,createBlock('jewellery/adminhtml_catalog_category_product_tab')无法正常工作。 createBlock()用于创建新块并将其添加到布局中。该功能有3个参数。

  1. type 2. name 3. attributes
  2. 您已将类型指定为jewellery/adminhtml_catalog_category_product_tab。 SO magento将查找app/code/local/Subora/Jewellery/Block/Adminhtml/Catalog/Category/Product/Tab.ph‌​p,并且应正确声明该文件。

    接下来,您需要指定块名称。有必要。 Magento需要所有块都有唯一的名称。这里没有姓名。

    下一个参数是属性。这是选择性的。您可以使用此参数设置模板。所以你可以试试这段代码。

    $this->getLayout()->createBlock(
        'jewellery/adminhtml_catalog_category_product_tab',
        'jewellery.tab',
         array('template' => 'your/template.phtml') //if any
    )->toHtml();
    

    修改

    首先,您需要确保您的模块是否处于活动状态。你可以通过管理员这样做。 System > Configuration > Advanced。检查你的模块在那里是否有效。

    接下来是,您的布局文件位置错误。 jewellery.xml是管理员布局文件。它应位于app\design\adminhtml\default\default\layout\jewellery.xml而不是app\design\frontend\base\default\layout\jewellery.xml