《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ì)與開(kāi)發(fā)中一個(gè)基本的需求與挑戰(zhàn):數(shù)據(jù)庫(kù)應(yīng)用,這些設(shè)計(jì)模式都沒(méi)有涉及到。本章與接下來(lái)的兩章—
回顧表數(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); } } |
更新記錄
下面,讓我們來(lái)解決CRUD中的“更新”。從概念上講,你應(yīng)該讓表裝滿(mǎn)數(shù)據(jù),找到一個(gè)數(shù)據(jù)對(duì)象,改變后保存它,并且再次找到該數(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來(lái)更新數(shù)據(jù),并能正確的將數(shù)據(jù)傳輸對(duì)象的屬性的值映射到SQL語(yǔ)句相應(yīng)的參數(shù)位置。
討論
用表數(shù)據(jù)網(wǎng)關(guān)在對(duì)表進(jìn)行操作,是與WEB應(yīng)用中任務(wù)的執(zhí)行更密切相關(guān)的。然而,表數(shù)據(jù)網(wǎng)關(guān)仍然與數(shù)據(jù)庫(kù)表具體結(jié)構(gòu)關(guān)系過(guò)于緊密(耦合)。將代碼從表具體結(jié)構(gòu)的依賴(lài)中獨(dú)立出來(lái)將是下一章數(shù)據(jù)映射模式的主題。
分享:《PHP設(shè)計(jì)模式介紹》第十三章 適配器模式接口的改變,是一個(gè)需要程序員們必須(雖然很不情愿)接受和處理的普遍問(wèn)題。程序提供者們修改他們的代碼;系統(tǒng)庫(kù)被修正;各種程序語(yǔ)言以及相關(guān)庫(kù)的發(fā)展和進(jìn)化。我孩子的無(wú)數(shù)玩具中有一個(gè)簡(jiǎn)要地描
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁(yè)面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪(fǎng)問(wèn)控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語(yǔ)言構(gòu)造器介紹
- php/js獲取客戶(hù)端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語(yǔ)言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- php配置文件php.ini的中文注釋版
- PHP技巧:用PHP導(dǎo)出MySQL數(shù)據(jù)庫(kù)內(nèi)容為.sql文件
- PHP session常見(jiàn)問(wèn)題集錦及解決辦法
- Zend的Registry機(jī)制的使用說(shuō)明
- 在動(dòng)態(tài)網(wǎng)頁(yè)技術(shù)PHP5中類(lèi)(CLASS)的新特征
- 基于PHP字符串的比較函數(shù)strcmp()與strcasecmp()的使用詳解
- 用php實(shí)現(xiàn)選擇排序的解決方法
- 深入PHP異步執(zhí)行的詳解
- php快速u(mài)rl重寫(xiě)實(shí)例
- 淺析PHP實(shí)現(xiàn)同步遠(yuǎn)程Mysql
- 相關(guān)鏈接:
復(fù)制本頁(yè)鏈接| 搜索《PHP設(shè)計(jì)模式介紹》第十五章 表數(shù)據(jù)網(wǎng)關(guān)模式(4)
- 教程說(shuō)明:
PHP教程-《PHP設(shè)計(jì)模式介紹》第十五章 表數(shù)據(jù)網(wǎng)關(guān)模式(4)
。