Magento中如何在模塊中使用多張數據表並配置多個model?

功能介紹:

引用magento開發人員的一句話:php

Magento has basic one resource to one table resource。mysql

也便是一個資源對應一張數據表。
當有時候,須要在一個模塊中使用多張數據表,配置多個model,那麼又該如何配置model呢?sql

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>";
        }
    }
}

在模板文件中輸出:app

<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>

數據庫內容所示:
圖片描述測試

輸出如圖所示:
圖片描述this

相關文章
相關標籤/搜索