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

标签 php mysql xml magento

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

我的命名空间是: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 的管理填充的

我可以看到,由于该 block 扩展了 Mage_Adminhtml_Block_Widget_Grid_Container 类,因此它本身会将此模块内的网格 block 添加为其子级。

我的配置设置 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>

我的系统.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 block

<?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');
    }
}

我的 block 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');
    }
}

表单.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 我的编辑表单显示,但我的网格不显示,这里有什么我遗漏的

最佳答案

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

Custom Module Creation Link

关于php - 网格不显示在自定义 magento 模块中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22069242/

相关文章:

php - 使用 Ajax 插入 mysql 数据库的问题

mysql - sqlite语法问题

html - 为什么我的 XPath 不基于 text() 进行选择?

python - 如何转义 xpath 中的正斜杠?

java - 由于重力作用,图像不在底部(底部)

php - 在 PHP 中正确扩展 ArrayObject?

php - JQUERY AJAX 使用 PHP 循环加载内容

php - 没有脚本标签 - 可靠吗?安全的?

php - MySQL选择两个字段不同时在数组中

php - 是否有一种标准方法来分离 Symfony2 应用程序的单元测试和功能测试?