Sağlıksız Dünyamız

Page 233

The Active Record Pattern

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); } }

This test passes an ID to the constructor, something the existing tests do not do. Passing an ID has to be optional, because existing tests that create new, empty Bookmark instances must continue to work. Here’s some code to realize the requirements of the test(s):

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 { >conn-> >errorMsg()); trigger_error(‘DB Error: ‘.$this-> } } } // ... }

235


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.