1

功能介绍:

引用magento开发人员的一句话:

Magento has basic one resource to one table resource。

也即是一个资源对应一张数据表。
当有时候,需要在一个模块中使用多张数据表,配置多个model,那么又该如何配置model呢?

1.创建数据表:

//创建第一张表
CREATE TABLE `custom` (
`custom_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR( 25 ) NOT NULL
PRIMARY KEY (`custom_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


//创建第二张表
CREATE TABLE `customdata` (
`customdata_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR( 25 ) NOT NULL
PRIMARY KEY (`customdata_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2.编写配置文件

<?xml version="1.0"?>
<config>
    <modules>
        <Test_Custom>
            <active>true</active>
            <codePool>local</codePool>
        </Test_Custom>
    </modules>
</config>

3.配置模块中配置文件etc/config.xml

//找到如下代码
......
<global>
    <models>
        <custom>
            <class>Test_Custom_Model</class>
            <resourceModel>custom_mysql4</resourceModel>
        </custom>
    
        <custom_mysql4>
            <class>Test_Custom_Model_Mysql4</class>
            <entities>
                //第一张表
                <custom><!--这个节点要求对应Model/Mysql4/Custom.php和Model/Mysql4/Custom/Custom.php中的_init('模块名/custom')这个参数,一般要求与表名相同-->
                    <table>custom</table>
                </custom>
                
                //第二张表
                <customdata><!--这个节点跟上面的功能一样-->
                    <table>customdata</table>
                </customdata>
            </entities>
        </custom_mysql4>
    </models>
    <resources>
        <custom_setup><!--这个节点对应sql/custom_setup文件夹的名称保持一致-->
            <setup>
                <module>Silk_Custom</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </custom_setup>
        <custom_write>
            <connection>
                <use>core_write</use>
            </connection>
        </custom_write>
        <custom_read>
            <connection>
                <use>core_read</use>
            </connection>
        </custom_read>
    </resources>
</global>
......

4.创建模型Custom.php和Customdata.php

图片描述

//app/code/local/Mypackage/Mymodule/Model/Custom.php
<?php
class Mypackage_Mymodule_Model_Test extends Mage_Core_Model_Abstract
{   
    public function _construct()
    {
        parent::_construct();
        $this->_init('mymodule/Custom');
    }
}
?>

//app/code/local/Mypackage/Mymodule/Model/Customdata.php
class Mypackage_Mymodule_Model_Test extends Mage_Core_Model_Abstract
{   
    public function _construct()
    {
        parent::_construct();
        $this->_init('mymodule/Customdata');
    }
}
?>

5.创建资源模型

//app/code/local/Mypackage/Mmodule/Model/Mysql4/Custom.php
<?php
class Mypackage_Mymodule_Model_Mysql4_Custom extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {    
        $this->_init('mymodule/custom', 'custom_id');//custom_id为表custom的主键
    }
}
?>
 

//app/code/local/Mypackage/Mmodule/Model/Mysql4/Customdata.php
<?php
class Mypackage_Mymodule_Model_Mysql4_Customdata extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {    
        $this->_init('mymodule/customdata', 'customdata_id');//customdata_id为表customdata的主键
    }
}
?>

6.创建一个集合类

//local/Mypackage/Mymodule/Model/Mysql4/Custom/Collection.php
<?php
class Mypackage_Mymodule_Model_Mysql4_Custom_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('mymodule/custom');
    }
}
?>
 
//local/Mypackage/Mymodule/Model/Mysql4/Customdata/Collection.php
<?php 
class Mypackage_Mymodule_Model_Mysql4_Customdata_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('mymodule/customdata');
    }
}
?>

图片描述

7.现在model就配置好了

model配置好后,来做下测试,我们在block中取得数据,然后从模板文件中调取,

<?php
class Mypackage_Mymodule_Block_Custom extends Mage_Core_Block_Template
{
    /*
     * 循环遍历出option
     */
    public function getcustom($field)
    {
        $customdata=Mage::getModel('custom/customdata')->getCollection()->addfieldtofilter('field',$field);//addfieldtofilter()为过滤条件
        $res=$customdata->getData();
        //print_r($customdata->getSelectSql(true));//打印出sql语句
        foreach ($res as $v)
        {
            $val=$v['val'];
            //echo $val;
            echo "<option value='$val'>$val</option>";
        }
    }
}

在模板文件中输出:

<select name="hook_load">
    <option value="">--Select--</option>
    <?php echo $this->getcustom('hook_load');?>//调用block中的返回的数据
</select>

<select name="mast">
    <option value="">--Select--</option>
    <?php echo $this->getcustom('mast');?>//调用block中的返回的数据
</select>

数据库内容所示:
图片描述

输出如图所示:
图片描述


denson
366 声望57 粉丝