Magento自定义后端模块网格排序/过滤问题

时间:2010-11-13 00:05:53

标签: php zend-framework magento

我正在开发一个自定义管理模块,我根据自定义属性显示客户列表,网格加载正常,但每当我尝试对网格进行排序/过滤时,我就遇到了问题。

这是我得到的错误:

Fatal error:  Call to a member function toHtml() on a non-object in <b>/***/***/public_html/***/app/code/local/BelVG/Events/controllers/CodesController.php</b> on line <b>28</b>

这是导致CodesController文件中的错误的代码:

public function customerGridAction() {
    $this->loadLayout();
    $this->getResponse()->setBody($this->getLayout()->getBlock('events.codes.edit.customer')->toHtml());
}

XML布局文件

<events_codes_edit>
    <reference name="content">
        <block type="events/codes_edit" name="events.codes.edit" template="events/codes/edit.phtml">
        <block type="events/codes_edit_customer" name="events.codes.edit.customer" as="customer"/>
         </block>
    </reference>
</events_codes_edit>

<events_codes_edit_customergrid>
    <remove name="root"/>
    <block type="events/codes_edit_customer" name="events.codes.edit.customer" as="events.codes.edit.customer"/>
</events_codes_edit_customergrid></code>

网格的类文件:

class BelVG_Events_Block_Codes_Edit_Customer extends Mage_Adminhtml_Block_Widget_Grid
{

public function __construct() {
    parent::__construct();
    $this->setId('events_codes_edit_product');
    $this->setUseAjax(true);
    $this->setDefaultSort('entity_id');
    $this->setDefaultDir('asc');
    $this->setSaveParametersInSession(true);
}

protected function _prepareCollection()
{
    $current_code = Mage::registry('current_code');
    $code = $current_code->getCode();
    $collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('i_code')
        ->addAttributeToSelect('gender')
        ->addFieldToFilter('i_code', $code)
        ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
        ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
        ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
        ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
        ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

    $this->setCollection($collection);

    return parent::_prepareCollection();
}

 protected function _prepareColumns() {
    $this->addColumn('entity_id', array(
        'header'    => Mage::helper('customer')->__('ID'),
        'width'     => '50px',
        'index'     => 'entity_id',            
        'type'  => 'number'
    ));
    $this->addColumn('name', array(
        'header'    => Mage::helper('customer')->__('Name'),
        'index'     => 'name'
    ));
    $this->addColumn('email', array(
        'header'    => Mage::helper('customer')->__('Email'),
        'width'     => '150',
        'index'     => 'email'
    ));
    $this->addColumn('Telephone', array(
        'header'    => Mage::helper('customer')->__('Telephone'),
        'width'     => '100',
        'index'     => 'billing_telephone'
    ));

    $this->addColumn('billing_postcode', array(
        'header'    => Mage::helper('customer')->__('ZIP'),
        'width'     => '90',
        'index'     => 'billing_postcode'
    ));

    $this->addColumn('billing_country_id', array(
        'header'    => Mage::helper('customer')->__('Country'),
        'width'     => '100',
        'index'     => 'billing_country_id'
    ));

    $this->addColumn('billing_region', array(
        'header'    => Mage::helper('customer')->__('State/Province'),
        'width'     => '100',
        'index'     => 'billing_region'
    ));

    $this->addColumn('gender', array(
        'header'    => Mage::helper('customer')->__('Gender'),
        'align'     => 'center',
        'index'     => 'gender'
    ));


    return parent::_prepareColumns();
}

public function getGridUrl() {
    return $this->getUrl('*/*/customergrid', array('_current'=> true));
}

}

此网格首先在另一个块内调用,即Edit.php,并从模板文件edit.phtml中调用

Edit.php阻止类

class BelVG_Events_Block_Codes_Edit extends Mage_Core_Block_Template {

    public function getGridHtml() {
        return $this->getChild('customer')->toHtml();
    }

}

在edit.phtml中调用客户网格的代码

<div id="" class="fieldset">
        <div class="hor-scroll">
            <?php echo $this->getGridHtml() ?>
        </div>
</div>

我不知道为什么会发生这种情况,我已经仔细检查了控制器中的块名称,并且布局文件和它们似乎匹配,我甚至尝试使用createBlock()代替getBlock()并直接指向到块文件,但它仍显示完全相同的错误。

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

我不确定您的模块/控制器/操作路径是什么样的,但我会说您在xml中出错:

<events_codes_edit_customergrid>

我猜这应该说

<events_codes_customergrid>

但无论如何。如果你想获得一个特定块的html,这不是真正需要的。我建议做的是,使用布局类创建块,然后显示它。

这通常适用于所有网格块,因为它们在类中定义了Collection实例。

所以尝试使用

$this->getResponse()->setBody($this->getLayout()->createBlock('events/codes_edit_customer')->toHtml());