• magento后台开发学习笔记(入门实例向)


    目的是做一个grid,参考博客http://www.sunzhenghua.com/magento-admin-module-development-part1-grid-forms-tabs-controllers,

    由于少了很多基础的配置,我便补充了一下

    一。

    开发的目录结构如下:Jago->Employee:

    第一步是建立module,在config.xml下进行配置:

    <config>
        <modules>
            <Jago_Employee>
                <version>1.0.0</version>
            </Jago_Employee>
        </modules>
    </config

    然后在app/etc/modules目录下建立Jago_Employee.xml文件:

    <config>
        <modules>
            <Jago_Employee>//取这个名字是与上面的目录对应,文件名同理
                <active>true</active>
                <codePool>local</codePool>
            </Jago_Employee>
        </modules>
    </config>

    完成这一步 可以进入后台System->Configuration->Advanced看看,在Disable Modules Output下面会有Jago_Employee 字段。第一步算是成功了。

    接下去是在后台导航条中插入菜单:

    依然是config.xml文件中加入如下代码

    <config>
       .....
      <adminhtml> <menu> <employee module="employee"> <title>Employee</title>//导航条上的名字 <sort_order>71</sort_order> <children> <items module="employee"> <title>Manage Employees</title> <sort_order>0</sort_order> <action>employee/adminhtml_employee</action>//指向Controller里的action </items>//若想加入多个子元素可写<item2></item2>之类 </children> </employee> </menu> </adminhtml>
      ....
    </config>

    如果这时候刷新的话就会报错

    Warning: include(MageEmployeeHelperData.php): failed to open stream: 
    嘛,根据提示是没有上面这个文件,于是继续在config.xml中加入配置
    <config>
    ...
        <global>
            <helpers>//这一段配置是导入Helper
                <employee>
                    <class>Jago_Employee_Helper</class>
                </employee>
            </helpers>
        </global>
    ...
    </config>            

    然后就是建立上面目录中的Data.php文件了,代码如下:

    class Jago_Employee_Helper_Data extends          
    Mage_Core_Helper_Abstract { }

    现在刷新就可以在上方导航条中看见新建的目录了,不过你点击的话,肯定是404错误。

    继续干活:首先是配置路径,告诉magento点菜单要跳到哪里,在config.xml文件下

    <config>
        ...
        <frontend>
            <routers>
                <employee>
                    <use>standard</use>
                    <args>
                        <module>Jago_Employee</module>
                        <frontName>employee</frontName>
                    </args>
                </employee>
            </routers>
        </frontend>
        ...
    </config>

    路径是配完后,往controllers目录下的EmployeeController.php加入如下代码

    class Jago_Employee_Adminhtml_EmployeeController extends Mage_Adminhtml_Controller_action{
        public function indexAction() {
            //$this->loadLayout();
            //$this->renderLayout();  注释是因为
             echo "啊呜~";
        }
    }     

    OK,至少是可以看到他已经工作了。

    工作进入第2部分,加入模板

    如果你把我注释那段不注释掉的话,就会发现,其实是有一层模板的,好吧,主要是因为我怕找不到输出的那段。

    在config.xml中

    <adminhtml>//加入到这个标签下
    ...
    <layout>
                <updates>
                    <employee>
                        <file>employee.xml</file>
                    </employee>
                </updates>
            </layout>
    ...
    </adminhtml>

    声明了这么一个模板文件,我们需要在app/design/adminhtml/default/default/layout/下创建employee.xml

    <layout version="0.1.0">
        <employee_adminhtml_employee_index> //声明作用域,就是controllers下面那个index函数
            <reference name="content">
                <block type="employee/adminhtml_employee" name="employee">//声明调用哪个Block,所以下面就该建一个Block了
                </block></reference>
        </employee_adminhtml_employee_index>
    </layout>

    找到Block/Adminhtml/Employee.php

    class Jago_Employee_Block_Adminhtml_Employee extends Mage_Adminhtml_Block_Widget_Grid_Container{
        public function __construct(){
            $this->_controller = 'adminhtml_employee';
            $this->_blockGroup = 'employee';
    /**
    上面2个参数主要是定位我们即将要写的Grid文件
    在它的父类中有个_prepareLayout() 方法
    它会将上面的2个变量设置成$this->_blockGroup.'/' . $this->_controller . '_grid'
    //即
    employee/adminhtml_employee_grid
    
    */
            $this->_headerText = Mage::helper('employee')->__('Employee M');
            $this->_addButtonLabel = Mage::helper('employee')->__('Add');
            parent::__construct();
        }
    }

    然后在Block/Adminhtml/Employee/Grid.php

    class Jago_Employee_Block_Adminhtml_Employee_Grid extends Mage_Adminhtml_Block_Widget_Grid {
        public function __construct(){
            parent::__construct();
          // HTML 代码中的 <div> 标签中 ID 的值,你在同一个页面中应用多个Grid表时,ID 的值必须是唯一
            $this->setId('employeeGrid');
        //设置默认排序的列
            $this->setDefaultSort('id');
        //默认的排序顺序,ASC(正序)或 DESC(倒序)
            $this->setDefaultDir('ASC');
        //这个设置用来保存你在 Grid 表中所做的操作到 Session 中, 比如说你在 Grid 表分页中的第2页或做了一些搜索筛选操作,当你刷新页面或返回到该页面时,你刚所做的操作依然存在,  页面不会返回到初始值或状态
            $this->setSaveParametersInSession(true);
        }
        protected function _prepareCollection()
        {
            $collection = Mage::getModel('employee/employee')->getCollection();
            $this->setCollection($collection);
            return parent::_prepareCollection();
        }
    
        protected function _prepareColumns()
        {
            $this->addColumn('id', array(
                'header'    =>    Mage::helper('employee')->__('ID'),//是列显示的名称
                'align'     =>    'right',
                'width'     =>    '10px',
                'index'     =>    'id',//是来自于我们 Collection 中的一个字段,这个“id”列是存在于我们 Collection 的模块中。
            ));
    
            $this->addColumn('name', array(
                'header'    =>   Mage::helper('employee')->__('Name'),
                'align'     =>   'left',
                'index'     =>   'name',
                'width'     =>   '50px',
            ));
    
            $this->addColumn('content', array(
                'header'    =>   Mage::helper('employee')->__('Description'),
                'width'     =>   '150px',
                'index'     =>   'content',
            ));
    
            return parent::_prepareColumns();
        }
    }

    继续回到配置文件config.xml去声明一下块(Block)文件

    <global>
    ...
        <blocks>
                <employee>
                    <class>Jago_Employee_Block</class>
                </employee>
            </blocks>
    ...
    <global>

    这时刷新就会报错了

    Warning: include(MageEmployeeModelEmployee.php): failed to open stream:

    原因是缺少Model,还是在config.xml下声明Model

    <global>
    ...
            <models>
                <employee>
                    <class>Jago_Employee_Model</class>
                    <resourceModel>employee_mysql4</resourceModel>//也是后面用的
                </employee>
    //下面这一段是与数据库相关联的操作,后面会用到,可以先不写
                <employee_mysql4>
                    <class>Jago_Employee_Model_Mysql4</class>
                    <entities>
                        <employee>
                                 <table>test</table>//填的是数据库的表名
                       </employee>
                    </entities>
                </employee_mysql4>
            </models>    
    ...
    <global>    

    然后在Model/Employee.php下

    class Jago_Employee_Model_Employee extends Mage_Core_Model_Abstract{
        public function _construct() {
            parent::_construct();
            $this->_init('employee/employee');//这一段是与Block/Adminhtml/Employee/Grid.php中的_prepareCollection()里的设置相对应,不写的话会报错,你可以去掉看看效果
    } }

    这时候刷新的话,页面已经出来了,不过渲染出来的只有Block/Adminhtml/Employee.php,而最重要的Grid.php却没有出来。

    问题是出在数据库的连接上。

    进入Model/Mysql4/Employee.php

    class Jago_Employee_Model_Mysql4_Employee
        extends Mage_Core_Model_Mysql4_Abstract
    {
        public function _construct()
        {
            $this->_init('employee/employee', 'id');
        }
    }

    进入Model/Mysql4/Employee/Collection.php

    class Jago_Employee_Model_Mysql4_Employee_Collection
        extends Mage_Core_Model_Mysql4_Collection_Abstract
    {
        public function _construct()
        {
            parent::_construct();
            $this->_init('employee/employee');
        }
    }

    做完以上这些,你会发现还是不可以,因为数据库里根本就没有test这个表,继续加

    在config.xml中

    <global>
    ...
     <resources>//数据库安装操作
                <employee_setup>
                    <setup>
                        <module>Jago_Employee</module>
                    </setup>
                    <connection>
                        <use>core_setup</use>
                    </connection>
                </employee_setup>
                <employee_write>
                    <connection>
                        <use>core_write</use>
                    </connection>
                </employee_write>
                <employee_read>
                    <connection>
                        <use>core_read</use>
                    </connection>
                </employee_read>
            </resources>
    ...
    </global>

    在sql/employee_setup/mysql4-install-1.0.0.php

    $installer = $this;
    $installer->startSetup();
    $installer->run("
    
    -- DROP TABLE IF EXISTS {$this->getTable('test')};
    CREATE TABLE {$this->getTable('test')} (
    `id` int(11) unsigned NOT NULL auto_increment,
    `name` varchar(255) NOT NULL default '',
    `content` text NOT NULL default '',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    ");
    
    $installer->endSetup();

    ok,这时候页面出来的效果该是这样的

    先就这样了,剩下的部分请自行参考博客:

    http://www.sunzhenghua.com/magento-admin-module-development-part2-grid-forms-tabs-addbutton

  • 相关阅读:
    开窗函数Over
    CodeSmith
    codeMatic代码生成器
    Mvc身份认证方式
    Neo4j入门详细教程
    pathlib路径问题
    python_跨文件二维全局变量传参
    .md图片链接转存并替换路径,及相关报错解决方法
    ERROR: column "xxxxxx" does not exist解决办法
    SCP远程传输文件
  • 原文地址:https://www.cnblogs.com/cangzhou/p/3746085.html
Copyright © 2020-2023  润新知