《PHP設(shè)計模式介紹》第十四章 動態(tài)記錄模式(3)_PHP教程
推薦:《PHP設(shè)計模式介紹》第十三章 適配器模式接口的改變,是一個需要程序員們必須(雖然很不情愿)接受和處理的普遍問題。程序提供者們修改他們的代碼;系統(tǒng)庫被修正;各種程序語言以及相關(guān)庫的發(fā)展和進(jìn)化。我孩子的無數(shù)玩具中有一個簡要地描
到目前為止,這些實驗已證明了以下事實:屬性可以被設(shè)置,save()能正常工作,$ID屬性已被置為1。讓我們進(jìn)一步的深入到數(shù)據(jù)表去驗證其它的屬性值也被正確的保存。
| class ActiveRecordTestCase extends UnitTestCase { // ... function testNew() { $link = new Bookmark; $link->url = ‘http://simpletest.org/’; $link->name = ‘SimpleTest’; $link->description = ‘SimpleTest project homepage’; $link->tag = ‘testing’; $link->save(); $this->assertEqual(1, $link->getId()); // fetch the table as an array of hashes $rs = $this->conn->getAll(‘select * from bookmark’); $this->assertEqual(1, count($rs), ‘returned 1 row’); foreach(array(‘url’, ‘name’, ‘description’, ‘tag’) as $key) { $this->assertEqual($link->$key, $rs[0][$key]); } } } |
以上突出顯示代碼的功能是獲取整個書簽表數(shù)據(jù)。GetAll()方法執(zhí)行查詢并返回結(jié)果集,該結(jié)果集是以數(shù)組形式存放的記錄的哈稀表。AssertEqual()方法驗證只有一條記錄存在于結(jié)果集中。通過foreach循環(huán)比較從數(shù)據(jù)表中取得記錄的字段與$link對象的屬性值是否一致。
上述代碼已能正常工作,但通過手工的方法設(shè)定屬性值去增加書簽表數(shù)據(jù)的方法還是略顯繁瑣。因此,為上述的案例增加一個方便(通用)的方法,來實現(xiàn)增加的新建書簽對象。
The ActiveRecordTestCase::add()方法帶有(處理)四個參數(shù),可建立與插入一個新的ActiveRecord書簽對象。如果你在后續(xù)實驗中要用到新創(chuàng)建的書簽對象,add()方法還可以在創(chuàng)建成功后返回它。
| class ActiveRecordTestCase extends UnitTestCase { // ... function add($url, $name, $description, $tag) { $link = new Bookmark; $link->url = $url; $link->name = $name; $link->description = $description; $link->tag = $tag; $link->save(); return $link; } } |
你完全可以在本實驗案例中寫一個測試方法來證明其可用性。
| class ActiveRecordTestCase extends UnitTestCase { // ... function testAdd() { $this->add(‘http://php.net’, ‘PHP’, ‘PHP Language Homepage’, ‘php’); $this->add(‘http://phparch.com’, ‘php|architect’, ‘php|arch site’, ‘php’); $rs = $this->conn->execute(‘select * from bookmark’); $this->assertEqual(2,$rs->recordCount()); $this->assertEqual(2,$this->conn->Insert_ID()); } } |
既然書簽可以創(chuàng)建并存儲于數(shù)據(jù)庫中,讓我們給Active Record書簽對象增加一個方法,可以簡單的從數(shù)據(jù)庫中獲取數(shù)據(jù)并在實例的屬性中保存所獲取的值。一種通用的建立動態(tài)記錄對象的技術(shù)是通過傳遞一個標(biāo)示符,如書簽號(或是別的什么標(biāo)準(zhǔn))到它的構(gòu)造函數(shù)中,并且從數(shù)據(jù)庫中取出與這個ID相關(guān)聯(lián)的行數(shù)據(jù)。
| class ActiveRecordTestCase extends UnitTestCase { // ... function testCreateById() { $link = $this->add( ‘http://blog.casey-sweat.us/’, ‘My Blog’, ‘Where I write about stuff’, ‘php’); $this->assertEqual(1, $link->getId()); $link2 = new Bookmark(1); $this->assertIsA($link2, ‘Bookmark’); $this->assertEqual($link, $link2); } } |
這個實驗傳遞了一個ID到構(gòu)造函數(shù),這是前面的實驗所沒有出現(xiàn)過的。是否傳遞ID是可選的,如果沒有傳遞ID,則前述試驗中建立新的空書簽實例的功能將依然正常工作。
這兒是一些實現(xiàn)上述功能要求的代碼。
| class Bookmark { // ... const SELECT_BY_ID = ‘select * from bookmark where id = ?’; public function __construct($id=false) { $this->conn DB::conn(); if ($id) { $rs = $this->conn->execute( self::SELECT_BY_ID ,array((int)$id)); if ($rs) { $row = $rs->fetchRow(); foreach($row as $field => $value) { $this->$field = $value; } } else { trigger_error(‘DB Error: ‘.$this->conn->errorMsg()); } } } // ... } |
構(gòu)造函數(shù)允許一個名為$id的參數(shù),它的默認(rèn)為假。如果傳來的參數(shù)不為假,則BOOKmark則用此ID為關(guān)鍵字查詢數(shù)據(jù)庫中BOOKmark表的相關(guān)行,如果該行存在,則用獲取的數(shù)據(jù)來設(shè)定對象屬性的值。
數(shù)據(jù)錯誤測試
| Mock::generate(‘ADOConnection’); class ActiveRecordTestCase extends UnitTestCase { //... function testDbFailure() { $conn = new MockADOConnection($this); $conn->expectOnce(‘execute’, array(‘*’,’*’)); $conn->setReturnValue(‘execute’,false); $conn->expectOnce(‘errorMsg’); $conn->setReturnValue(‘errorMsg’, ‘The database has exploded!!!!’); } } |
這段代碼調(diào)用了Mock::generate() 來生成一個MockADOConnection 類,并生成一個模擬連接的實例,同時設(shè)定一些基本的返回值來指明錯誤,和定義在這些環(huán)境中可能會出現(xiàn)的意外。
分享:《PHP設(shè)計模式介紹》第十二章 裝飾器模式若你從事過面向?qū)ο蟮膒hp開發(fā),即使很短的時間或者僅僅通過本書了解了一些,你會知道,你可以 通過繼承改變或者增加一個類的功能,這是所有面向?qū)ο笳Z言的一個基本特性。如果已經(jīng)存在的一個php
- 相關(guān)鏈接:
- 教程說明:
PHP教程-《PHP設(shè)計模式介紹》第十四章 動態(tài)記錄模式(3)
。