2012-07-16 18 views

回答

2

http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics/

首先你將需要/寫節添加設置/讀給你的config.xml。比方說,你的模塊測試/演示那麼你的設置部分可能看起來有點像這樣:

<models> 
    <demo> 
     <class>Test_demo_Model</class> 
     <resourceModel>Demo_mysql4</resourceModel> 
    </demo> 
    <demo_mysql4> 
     <class>Test_Demo_Model_Mysql4</class> 
     <entities> 
      <demo> 
       <table>test_demo</table> 
      </demo> 
     </entities> 
    </demo_mysql4> 
    </models> 
    <resources> 
    <demo_setup> 
     <setup> 
      <module>Test_demo</module> 
      <class>Test_Demo_Model_Resource_Mysql4_Setup</class> 
     </setup> 
     <connection> 
      <use>core_setup</use> 
     </connection> 
    </demo_setup> 
    <demo_write> 
     <connection> 
      <use>core_write</use> 
     </connection> 
    </demo_write> 
    <demo_read> 
     <connection> 
      <use>core_read</use> 
     </connection> 
    </demo_read> 
    </resources> 

在這一點上,我們需要創建初始化模型的Magento加載它。 'demo/_'可以是'demo/whateveryouwant',只要你在整個模塊中保持同樣的想法。 'id'是magento用於此模型的主鍵和標識符。

//Test/Demo/Model/Mysql4/Comment.php 
class Test_Demo_Model_Mysql4_Comment extends Mage_Core_Model_Mysql4_Abstract 
{ 
    protected function _construct() 
    { 
     $this->init('demo/________', 'id'); 
    { 
} 

從這裏您將需要創建數據庫安裝腳本。這可以通過簡單地創建文件Test/Demo/sql/demo_setup/mysql4-install-0.1.0.php來完成,其中0.1.0是配置文件中使用的版本號。它會是這個樣子:

$installer = $this; 
$installer->startSetup() 
$installer->run(" 
    #your create table goes here 
"); 
$installer->endSetup(); 

這將完成是創建表時,可以使用

CREATE TABLE {$installer ->getTable('demo/_____') as defined in your configuration file to create the table name used in the file. This will also create an entry in the table core_resource that will specify the name and version number. In order to make a modification to the table you'll need to delete the original table as well as it's entry in core_resource. At this point you'll want to create a model to manage the data. Here's an example of that for a table that looks like: 

//comment -String 
//poster -String 
//Id  -int autoincrement 
public function addComment($comment, $poster) 
{ 
    $comment = Mage::getModel('Demo/______'); 
    $comment->setComment($comment); 
    $comment->setPoster($poster); 
    $comment->save(); 
} 

對於列名,如poster_id您將使用setPosterId。使用駱駝案件,每封帽子信件都事先標明下劃線。

Poster_Id - > PosterId posterid - > Posterid

從數據庫中獲取值:

//using the same database example as above 
public function getAllByPoster($poster) 
{ 
    $collection = Mage::getModel('Demo/________')->getCollection(); 
    $collection->addFilter('poster', $poster); 
    return collection; 
} 

這將通過特定的海報返回所有的帖子。有一個問題,但沒有爲這個類定義集合。在我們看到如何顯示getAllByPoster的結果之前,我們有最後一個文件要創建。

//Test/Demo/Model/Mysql4/Comment/Collection.php 
class Test_Demo_Model_Mysql4_Comment_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract 
{ 
    protected function _construct() 
    { 
     $this->_init('comments/comment'); 
    } 
} 

在這一點上,我們有我們需要使用magento的類讀取和寫入數據庫的一切。要打印收藏,我們只需:

foreach (Mage::getModel('demo/_____')->getAllByPoster($id) as $something) 

並顯示我們希望從中選擇的各個屬性。

+0

我能弄明白。感謝這個鏈接。 – user1370946 2012-07-16 16:44:00