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

《PHP設(shè)計(jì)模式介紹》第十五章 表數(shù)據(jù)網(wǎng)關(guān)模式(4)_PHP教程

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

推薦:《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 {
// ...
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);
}
}

有了這樣一個(gè)實(shí)驗(yàn)用例在手,現(xiàn)是在增加update()方法到BookmarkGateway類(lèi)的時(shí)候了。

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)要地描

共4頁(yè)上一頁(yè)1234下一頁(yè)
來(lái)源:模板無(wú)憂(yōu)//所屬分類(lèi):PHP教程/更新時(shí)間:2008-08-22
相關(guān)PHP教程