Magento – 模型類如何獲得資源模型得執行個體

來源:互聯網
上載者:User
1. etc/config.xml有如下配置
<global>
<models>
<hotel>
<class>Cartz_Hotel_Model</class>
<resourceModel>hotel_mysql4</resourceModel>
</hotel>
<hotel_mysql4>
<class>Cartz_Hotel_Model_Mysql4</class>
<entities>
<room>
<table>rooms</table>
</room>
</entities>
</hotel_mysql4>
</models>
<resources>
<hotel_setup>
<setup>
<module>Cartz_Hotel</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</hotel_setup>
<hotel_write>
<connection>
<use>core_write</use>
</connection>
</hotel_write>
<hotel_read>
<connection>
<use>core_read</use>
</connection>
</hotel_read>
</resources>
</global>

2. 模型類為model/Room.php

<?php
class Cartz_Hotel_Model_Room extends Mage_Core_Model_Abstract{
protected function _construct() {
$this->_init('hotel/room');
}
public function listing(){
$this->_getResource()->findAll();
}
}
?>
3. 相應的資源模型類是model/Mysql4
<?php
class Cartz_Hotel_Model_Mysql4_Room extends Mage_Core_Model_Mysql4_Abstract{
protected function _construct(){
   $this->_init('hotel/room', 'id');
}
public function findAll() {
   $handle = $this->_getWriteAdapter();
   $query = $handle->query('select name from rooms');
   while ($row = $query->fetch()) {
$row = new Varien_Object($row);
      echo $row->getName() . "<br/>";
}
}
}
?>
這段代碼昭示了兩個知識點:

1. model類的方法_getResource()獲得相應資源模型類的執行個體。
2. 在資源模型類中, 方法 _getWriteAdapter()用來獲得資料庫的串連控制代碼

為了您的安全,請只開啟來源可靠的網址

開啟網站
    取消

來自: http://hi.baidu.com/pw425/blog/item/9a2a6a1d401ecd8286d6b64d.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.