日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

《PHP設計模式介紹》第十五章 表數據網關模式(4)_PHP教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:《PHP設計模式介紹》第十四章 動態(tài)記錄模式
到目前為止,您所看到的這些設計模式大大提高了代碼的可讀性與可維護性。然而,在WEB應用設計與開發(fā)中一個基本的需求與挑戰(zhàn):數據庫應用,這些設計模式都沒有涉及到。本章與接下來的兩章—

回顧表數據網關,你應該理解findByTage()的工作原理了。

class BookmarkGateway {
// ...
public function findByTag($tag) {
$rs = $this->conn->execute(
‘select * from bookmark where tag like ?’
,array($tag.’%’));
return new AdoResultSetIteratorDecorator($rs);
}
}

更新記錄

下面,讓我們來解決CRUD中的“更新”。從概念上講,你應該讓表裝滿數據,找到一個數據對象,改變后保存它,并且再次找到該數據并校檢更改是否存儲。

返回到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);
}
}

改變后,重新查找該條記錄并驗證更新

class TableDataGatewayTestCase extends BaseTestCase {
// ...
function testUpdate() {
The Table Data Gateway Pattern 257
$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);
$result = $gateway->findByTag(‘php’);
$bookmark = $result->current();
$this->assertEqual(
‘http://blog.casey-sweat.us/’
,$bookmark->url);
$this->assertEqual(
$new_desc
,$bookmark->description);
}
}

有了這樣一個實驗用例在手,現是在增加update()方法到BookmarkGateway類的時候了。

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來更新數據,并能正確的將數據傳輸對象的屬性的值映射到SQL語句相應的參數位置。

討論

用表數據網關在對表進行操作,是與WEB應用中任務的執(zhí)行更密切相關的。然而,表數據網關仍然與數據庫表具體結構關系過于緊密(耦合)。將代碼從表具體結構的依賴中獨立出來將是下一章數據映射模式的主題。

分享:《PHP設計模式介紹》第十三章 適配器模式
接口的改變,是一個需要程序員們必須(雖然很不情愿)接受和處理的普遍問題。程序提供者們修改他們的代碼;系統(tǒng)庫被修正;各種程序語言以及相關庫的發(fā)展和進化。我孩子的無數玩具中有一個簡要地描

共4頁上一頁1234下一頁
來源:模板無憂//所屬分類:PHP教程/更新時間:2008-08-22
相關PHP教程