网格不会显示在自定义magento模块中

时间:2014-02-27 12:58:01

标签: php mysql xml magento

我正在尝试在magento admin中创建自定义模块。我已经达到了将新链接添加到菜单并通过单击它的程度,我可以导航到模块控制器的索引操作。但是在这里我看不到网格,只显示了标题文本和块结构中添加的按钮。

我的命名空间是: Mmnamespace
我的模块是: Mmmodule

我还使用以下命令

创建了一个名为mmmodule_event的表
CREATE TABLE mmmodule_event (
    `event_id` INTEGER AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(255),
    `start` DATETIME,
    `end` DATETIME,
    `created_at` DATETIME,
    `modified_at` DATETIME
);

目前我使用magento的admin

填充了表中的两个项目

我可以看到,由于此块扩展了Mage_Adminhtml_Block_Widget_Grid_Container类,它本身会将此模块中的网格块添加为其子节点。

我的配置设置xml文件如下:

我的config.xml

    <?xml version="1.0"?>
<!DOCTYPE config>
<!--
/**
 * app/code/local/Mmnamespace/Mmmodule/etc/config.xml
 *
 *
 *
 *
 * @author    Omatsola Isaac Sobotie <tsola2002@yahoo.co.uk>
 * @category  Mmnamspace
 * @package   Mmmodule
 *
 */
-->
<config>
    <modules>
        <Mmnamespace_Mmmodule>
            <version>0.0.0</version>
        </Mmnamespace_Mmmodule>
    </modules>
    <global>
        <!--  this code tells magento to use resources  -->
        <models>
            <mmmodule>
                <class>Mmnamespace_Mmmodule_Model</class>
                <resourceModel>mmmodule_resource</resourceModel>
            </mmmodule>
            <mmmodule_resource>
                <class>Mmnamespace_Mmmodule_Model_Resource</class>
                <entities>
                    <event>
                        <table>mmmodule_event</table>
                    </event>
                    <event_registrant>
                        <table>mmmodule_event_registrant</table>
                    </event_registrant>
                </entities>
            </mmmodule_resource>
        </models>
        <blocks>
            <mmmodule>
                <class>Mmnamespace_Mmmodule_Block</class>
            </mmmodule>
        </blocks>
        <helpers>
            <mmmodule>
                <class>Mmnamespace_Mmmodule_Helper</class>
            </mmmodule>
        </helpers>
        <!--  initializing a predispatch observer gets fired anytime a controller is about to render an action -->
        <events>
            <controller_action_predispatch>
                <observers>
                    <mmmodule_observer>
                        <class>mmmodule/observer</class>
                        <method>controllerActionPredispatch</method>
                    </mmmodule_observer>
                </observers>
            </controller_action_predispatch>
        </events>
    </global>

    <!--  routing front page menu to appropriate controller -->
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <mmmodule before="Mage_Adminhtml">Mmnamespace_Mmmodule_Adminhtml</mmmodule>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>

    <!--used to route urls with module name to module-->
    <frontend>
        <routers>
            <mmmodule>
                <use>standard</use>
                <args>
                    <frontName>mmmodule</frontName>
                    <module>Mmnamespace_Mmmodule</module>
                </args>
            </mmmodule>
        </routers>

        <layout>
            <updates>
                <mmmodule>
                    <file>mmmodule.xml</file>
                </mmmodule>
            </updates>
        </layout>
    </frontend>
</config>

我的adminhtml.xml

<?xml version="1.0"?>
<!DOCTYPE config>
<!--
/**
 * app/code/local/Mmnamespace/Mmmodule/etc/adminhtml.xml
 *
 *

 * @category  Mmnamespace
 * @package   Mmmodule
 */
-->
        <!--  this code will process urls with that front name  -->
<config>
    <menu>
        <mmmodule translate="title" module="mmmodule">
            <title>Events</title>
            <sort_order>1000</sort_order>
            <action>adminhtml/event</action>
        </mmmodule>
    </menu>
</config>

我的system.xml

<?xml version="1.0"?>
<!DOCTYPE config>
<!--
/**
 * app/code/local/Mmnamespace/Mmmodule/etc/system.xml
 *
 *
 * @category  Mmnamespace
 * @package   Mmmodule
 */
-->
        <!--  this code adds a fieldset to an existing general section  -->
<config>
    <sections>
        <general translate="label">
            <groups>
                <mmmodule translate="label">
                    <label>Mmmodule Options</label>
                    <sort_order>0</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <some_field translate="label">
                            <label>Mmmodule Field</label>
                            <frontend_type>text</frontend_type> <!-- More frontend types can be found in the lib/Varien/Data/Form/Element folder -->
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </some_field>
                    </fields>
                </mmmodule>
            </groups>
        </general>
    </sections>
</config>

我的Mmnamespace_Mmmodule.xml

<?xml version="1.0"?>
<!DOCTYPE config>
<!--
/**
 * app/etc/modules/Mmnamespace_Mmmodule.xml
 *
 *
 *
 *
 * @author    Omatsola Isaac Sobotie <tsola2002@yahoo.co.uk>
 * @category  Mmnamespace
 * @package   Mmodule
 *
 */
-->
<config>
    <modules>
        <Mmnamespace_Mmmodule>
            <active>true</active>
            <codePool>local</codePool>
            <depends />
        </Mmnamespace_Mmmodule>>
    </modules>
</config>

我的EventController.php

<?php
/**
 *
 * Package: magentocore
 * Filename: MmmoduleController.php
 * Author: solidstunna101
 * Date: 25/02/14
 * Time: 18:40
 * * app/code/local/Mmnamespace/controllers/Adminhtml/EventController.php
 */

class Mmnamespace_Mmmodule_Adminhtml_EventController extends Mage_Adminhtml_Controller_Action
{
    //this function adds block content to main layout
    public function indexAction()
    {
        $this->loadLayout();
        $this->_setActiveMenu('mmmodule/events');

        /*$this->_addContent(
            $this->getLayout()->createBlock('mmmodule/adminhtml_event_edit')
 );*/

        $this->_addContent(
            $this->getLayout()->createBlock('mmmodule/adminhtml_event')
 );

        return $this->renderLayout();
    }

    public function saveAction()
    {
        //gathering form field parameters from the url
        $eventId = $this->getRequest()->getParam('event_id');
        $eventModel = Mage::getModel('mmmodule/event')->load($eventId);

        //save event to database
        if ( $data = $this->getRequest()->getPost() ) {
            try {
                $eventModel->addData($data)->save();
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    $this->__("Your event has been saved!")
                );
            } catch ( Exception $e ) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }

        $this->_redirect('*/*/index');
    }
}

我的event.php阻止

<?php
/**
 *
 * Package: magentocore
 * Filename: Event.php
 * Author: solidstunna101
 * Date: 27/02/14
 * Time: 08:14
 * Location:  app/code/local/Mmnamespace/Mmmodule/Block/Adminhtml/Event.php
 */

class Mmnamespace_Mmmodule_Block_Adminhtml_Event extends Mage_Adminhtml_Block_Widget_Grid_Container {


    public function __construct(){

        $this->_blockGroup = 'mmmodule';
        $this->_controller = 'adminhtml_event';
        $this->_headerText = Mage::helper('mmmodule')->__('Events');
        $this->_addButtonLabel = Mage::helper('mmmodule')->__('Add New Event');
        parent::__construct();
    }
}

我的Grid.php

<?php
/**
 *
 * Package: magentocore
 * Filename: Grid.php
 * Author: solidstunna101
 * Date: 27/02/14
 * Time: 08:27
 * Location: app/code/local/Mmnamespace/Mmmodule/Block/Adminhtml/Event/Grid.php
 */

class Mmnamespace_Mmmodule_Block_Adminhtml_Event_Grid extends Mage_Adminhtml_Block_Widget_Grid {

    protected function _prepareColumns()
    {
        $this->addColumn('name', array(
            'type' => 'text',
            'index' => 'name',
            'header' => $this->__('Name')
        ));

        $this->addColumn('start', array(
            'type' => 'date',
            'index' => 'start',
            'header' => $this->__('Start Date')
        ));

        $this->addColumn('end', array(
            'type' => 'date',
            'index' => 'end',
            'header' => $this->__('End Date')
        ));

        return $this;
    }


    public function _prepareCollection()
    {
        $collection = Mage::getModel('mmmodule/event')->getCollection();
        $this->setCollection($collection);

        return parent::_prepareCollection();

    }




}

我的资源event.php

<?php
/**
 *
 * Package: magentocore
 * Filename: Event.php
 * Author: solidstunna101
 * Date: 26/02/14
 * Time: 08:30
 * Location:  app/code/local/Mmnamespace/Mmmodule/Model/Event.php 
 */

class Mmnamespace_Mmmodule_Model_Event extends Mage_Core_Model_Abstract
{
    //this function initializes resources to be used
    public function _construct()
    {
        $this->_init('mmmodule/event');
    }
}

我的资源事件abstract.php

<?php
/**
 *
 * Package: magentocore
 * Filename: Event.php
 * Author: solidstunna101
 * Date: 26/02/14
 * Time: 08:33
 *Location:  app/code/local/Mmnamespace/Mmmodule/Model/Resource/Event.php
 */

class Mmnamespace_Mmmodule_Model_Resource_Event extends Mage_Core_Model_Resource_Db_Abstract
{
    //initializes primary key in the table
    public function _construct()
    {
        $this->_init('mmmodule/event', 'event_id');
    }
}

我的块edit.php

<?php
/**
 *
 * Package: magentocore
 * Filename: Edit.php
 * Author: solidstunna101
 * Date: 26/02/14
 * Time: 14:31
 * Location: app/code/local/Mmnamespace/Mmmodule/Block/Adminhtml/Event/Edit.php
 */

class Mmnamespace_Mmmodule_Block_Adminhtml_Event_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    public function __construct()
    {
        //creating components required to render the form
        $this->_objectId = 'event_id';
        $this->_blockGroup = 'mmmodule';
        $this->_controller = 'adminhtml_event';

        parent::__construct();
    }

    /**
     * Get edit form container header text
     *
     * @return string
     */
    public function getHeaderText()
    {
        return Mage::helper('mmmodule')->__('New Event');
    }

    public function getSaveUrl()
    {
        return $this->getUrl('*/event/save');
    }
}

form.php

<?php
/**
 *
 * Package: magentocore
 * Filename: Form.php
 * Author: solidstunna101
 * Date: 26/02/14
 * Time: 14:34
 * Location:  app/code/local/Mmnamespace/Mmmodule/Block/Adminhtml/Event/Edit/Form.php
 */

class Mmnamespace_Mmmodule_Block_Adminhtml_Event_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{

    //this function will override the prepare form function add data forms/fieldset
    public function _prepareForm()
    {
        $form = new Varien_Data_Form(
            array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post')
        );

        $fieldset = $form->addFieldset('base_fieldset', array('legend' => Mage::helper('mmmodule')->__('General Information'), 'class' => 'fieldset-wide'));

        $fieldset->addField('name', 'text', array(
            'name'      => 'name',
            'label'     => Mage::helper('mmmodule')->__('Event Name'),
            'title'     => Mage::helper('mmmodule')->__('Event Name'),
            'required'  => true
        ));

        $dateFormatIso = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
        $fieldset->addField('start', 'date', array(
            'name'      => 'start',
            'format'    => $dateFormatIso,
            'image'     => $this->getSkinUrl('images/grid-cal.gif'),
            'label'     => Mage::helper('mmmodule')->__('Start Date'),
            'title'     => Mage::helper('mmmodule')->__('Start Date'),
            'required'  => true
        ));

        $fieldset->addField('end', 'date', array(
            'name'      => 'end',
            'format'    => $dateFormatIso,
            'image'     => $this->getSkinUrl('images/grid-cal.gif'),
            'label'     => Mage::helper('mmmodule')->__('End Date'),
            'title'     => Mage::helper('mmmodule')->__('End Date'),
            'required'  => true
        ));

        $form->setUseContainer(true);
        $this->setForm($form);
    }
}

P.S我的编辑表单显示但我的网格没有显示,这里有什么我不知道

3 个答案:

答案 0 :(得分:0)

请按照以下链接创建前端和管理模块。 它可以清晰地工作,而不会在管理网格中出现任何问题。

Custom Module Creation Link

答案 1 :(得分:0)

尝试在Grid.php _prepareColumns中调用parent :: _ prepareColumns。如果不调用父网格,可能无法触发您的网格。

P.S。在我的管理模块中,我实际上返回parent :: _ prepareColumns而不是$ this。

答案 2 :(得分:0)

Mmnamespace_Mmmodule_Adminhtml_EventController中将return $this->renderLayout();更改为$this->renderLayout();

protected function indexAction()
{   
    $this->loadLayout()
        ->_setActiveMenu('mmmodule/items')
        ->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));

    $this->_addContent($this->getLayout()->createBlock('mmmodule/adminhtml_event'));
    $this->renderLayout();  //<-- not return $this->renderLayout();
}

看看@ Custom_module_with_custom_database_table,因为您的代码似乎还有其他小问题