《PHP設(shè)計(jì)模式介紹》第十五章 表數(shù)據(jù)網(wǎng)關(guān)模式(4)_PHP教程
推薦:《PHP設(shè)計(jì)模式介紹》第十四章 動(dòng)態(tài)記錄模式到目前為止,您所看到的這些設(shè)計(jì)模式大大提高了代碼的可讀性與可維護(hù)性。然而,在WEB應(yīng)用設(shè)計(jì)與開發(fā)中一個(gè)基本的需求與挑戰(zhàn):數(shù)據(jù)庫應(yīng)用,這些設(shè)計(jì)模式都沒有涉及到。本章與接下來的兩章—
回顧表數(shù)據(jù)網(wǎng)關(guān),你應(yīng)該理解findByTage()的工作原理了。
| class BookmarkGateway { // ... public function findByTag($tag) { $rs = $this->conn->execute( ‘select * from bookmark where tag like ?’ ,array($tag.’%’)); return new AdoResultSetIteratorDecorator($rs); } } |
更新記錄
下面,讓我們來解決CRUD中的“更新”。從概念上講,你應(yīng)該讓表裝滿數(shù)據(jù),找到一個(gè)數(shù)據(jù)對象,改變后保存它,并且再次找到該數(shù)據(jù)并校檢更改是否存儲(chǔ)。
返回到TableDataGatewayTestCase,這兒有查找記錄的代碼
| class TableDataGatewayTestCase extends BaseTestCase { // ... function testUpdate() { $gateway = new BookmarkGateway(DB::conn()); $this->addSeveralBookmarks($gateway); $result = $gateway->findByTag(‘php’); $bookmark = $result->current(); $this->assertIsA($bookmark, ‘ADOFetchObj’); $this->assertEqual( ‘http://blog.casey-sweat.us/’ ,$bookmark->url); $this->assertEqual( ‘PHP related thoughts’ ,$bookmark->description); } } |
并且將代碼改為如下所示:
| class TableDataGatewayTestCase extends BaseTestCase { // ... function testUpdate() { $gateway = new BookmarkGateway(DB::conn()); $this->addSeveralBookmarks($gateway); $result = $gateway->findByTag(‘php’); $bookmark = $result->current(); $this->assertIsA($bookmark, ‘ADOFetchObj’); $this->assertEqual( ‘http://blog.casey-sweat.us/’ ,$bookmark->url); $this->assertEqual( ‘PHP related thoughts’ ,$bookmark->description); $new_desc = ‘A change to see it is updated!’; $bookmark->description = $new_desc; $gateway->update($bookmark); } } |
改變后,重新查找該條記錄并驗(yàn)證更新
|
class TableDataGatewayTestCase extends BaseTestCase { |
| class BookmarkGateway{ // ... const UPDATE_SQL = ‘update bookmark set url = ? ,name = ? ,description = ? ,tag = ? ,updated = now() where id = ?’; public function update($bookmark) { $this->conn->execute( self::UPDATE_SQL ,array( $bookmark->url ,$bookmark->name ,$bookmark->description ,$bookmark->tag ,$bookmark->id )); } |
BookmarkGateway知道如何去執(zhí)行SQL來更新數(shù)據(jù),并能正確的將數(shù)據(jù)傳輸對象的屬性的值映射到SQL語句相應(yīng)的參數(shù)位置。
討論
用表數(shù)據(jù)網(wǎng)關(guān)在對表進(jìn)行操作,是與WEB應(yīng)用中任務(wù)的執(zhí)行更密切相關(guān)的。然而,表數(shù)據(jù)網(wǎng)關(guān)仍然與數(shù)據(jù)庫表具體結(jié)構(gòu)關(guān)系過于緊密(耦合)。將代碼從表具體結(jié)構(gòu)的依賴中獨(dú)立出來將是下一章數(shù)據(jù)映射模式的主題。
分享:《PHP設(shè)計(jì)模式介紹》第十三章 適配器模式接口的改變,是一個(gè)需要程序員們必須(雖然很不情愿)接受和處理的普遍問題。程序提供者們修改他們的代碼;系統(tǒng)庫被修正;各種程序語言以及相關(guān)庫的發(fā)展和進(jìn)化。我孩子的無數(shù)玩具中有一個(gè)簡要地描
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
復(fù)制本頁鏈接| 搜索《PHP設(shè)計(jì)模式介紹》第十五章 表數(shù)據(jù)網(wǎng)關(guān)模式(4)
- 教程說明:
PHP教程-《PHP設(shè)計(jì)模式介紹》第十五章 表數(shù)據(jù)網(wǎng)關(guān)模式(4)
。