首先需要在配置文件config.xml裏面寫:
<resources>
<tdata_setup>
<setup>
<module>Dipper_Tdata</module>
<class>Mage_Core_Model_Resource_Setup</class>
</setup>
<connection>
<use>default_setup</use>
</connection>
</tdata_setup>
</resources>
這段主要是調用默認方法安裝
在相應的模塊裏面sql\tdata_setup\mysql4-install-1.0.0.php
<?php
/**
*/
//die('Dipper'); //可以啓用這個註釋。這樣就可以看到這個文件是否被調用。如果調用。打開頁面時。會顯示Dipper
$installer = $this;
$installer->startSetup();
$installer->run("
CREATE TABLE IF NOT EXISTS `{$this->getTable('dipper')}` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` CHAR(20) DEFAULT NULL,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MYISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;
");
$installer->endSetup();
// EOF
根據上面的內容設置。如果數據庫沒有生存你要的數據表。有兩種可能:
第一:你配置內容寫錯了,或者你插入的數據庫信息有誤。
第二:就是你已經安裝過這個方法。但是再次安裝就不行,必須先刪除core_resource裏的一條記錄
SELECT * FROM core_resource WHERE CODE='tdata_setup'
//因爲我的數據庫安裝文件是tdata_setup 所以我查code='tdata_setup'
DELETE FROM core_resource WHERE CODE='tdata_setup'
//刪除這個。就可以重新安裝,安裝成功之後。表裏會生存相應的記錄。防止多次安裝。
想要查找,修改這個表的數據,在config.xml文件models裏面添加如下內容:
<tdata_mysql4>
<class>Dipper_Tdata_Model_Resource_Mysql4</class>
<entities>
<dipper>
<table>zdatatable</table>
</dipper>
</entities>
</tdata_mysql4>
然後再model文件夾下添加Resource文件夾,再在裏面添加mysql4的文件夾
mysql4文件夾下增加Dipper.php和Dipper文件夾
Dipper文件夾下增加Collection.php
Dipper.php文件的內容:
class Dipper_Tdata_Model_Resource_Mysql4_Dipper extends Mage_Core_Model_Mysql4_Abstract { protected function _construct() { $this->_init('tdata/dipper', 'id'); } }
Collection.php內容:
class Dipper_Tdata_Model_Resource_Mysql4_Dipper_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
protected function _construct()
{
$this->_init('tdata/dipper');
}
}
在model文件下添加一個Tdata.php的model文件:
class Dipper_Tdata_Model_Tdata extends Mage_Core_Model_Abstract
{
protected function _construct()
{
$this->_init('tdata/dipper');
}
}
添加完上面的就可以通過下面方法查找和修改表數據了:
$test = Mage::getModel('tdata/tdata')->getCollection();
print_r($test->getData()); //查找數據內容
$test = Mage::getModel('tdata/tdata');
$test->setName('1')->save();
//插入數據內容。修改的話,把要修改的內容查詢數據。然後SET->SAVE。
希望對大家有幫助。自己總結的