Home arrow PHP arrow Page 2 - The Active Record Pattern, concluded

Active Record Instance ID - PHP

This article, the second of two parts, helps you use design patterns to better organize how your web application interacts with a database. It is excerpted from chapter 14 of the book php|architect's Guide to PHP Design Patterns, written by Jason E. Sweat (php|architect, 2005; ISBN: 0973589825).

TABLE OF CONTENTS:
  1. The Active Record Pattern, concluded
  2. Active Record Instance ID
  3. Searching for Records
  4. Updating Records
  5. Issues
By: php|architect
Rating: starstarstarstarstar / 2
December 29, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the previous example, most of the attributes are public; however, the ID of the bookmark is protected to avoid accidents changing its value (this would be problematic when you wanted to later update the bookmark). Since $id is protected, add an accessor method to retrieve it from the Bookmark.

class Bookmark {
  protected $id;
  //...
  public function getId() {
    return $this->id;
  }
}

How do you test this?

class ActiveRecordTestCase extends UnitTestCase {
  // ...
  function testGetId() {
    $this->add(‘http://php.net’, ‘PHP’,
      ‘PHP Language Homepage’, ‘php’);
    // second bookmark, id=2
    $link = $this->add(‘http://phparch.com’,
      ‘php|architect’, ‘php|arch site’, ‘php’);
    $this->assertEqual(2, $link->getId());
  }
} 

Immediately above, add() persists several bookmarks and verifies that the latter of the two matches.

So far, so good, but what if you want to verify the database entry based on a different criteria than the bookmark ID? How can  you make sure the correct ID from the database is being returned? A good technique is to SELECT from the database using a known attribute and verify the ID from the returned row. Here’s a test using this methodology:

class ActiveRecordTestCase extends UnitTestCase {
  // ...
  function testGetId() {
    $this->add(‘http://php.net’, ‘PHP’, 
      ‘PHP Language Homepage’, ‘php’);
    // second bookmark, id=2
    $link = $this->add(‘http://phparch.com’,
      ‘php|architect’, ‘php|arch site’, ‘php’);
    $this->assertEqual(2, $link->getId());
    $alt_test = $this->conn->getOne(
      “select id from bookmark where url = ‘http://phparch.com’”);
    $this->assertEqual(2, $alt_test);
    //alternatively
    $this->assertEqual($link->getId(), $alt_test);
  }
} 

Notice that this test resembles the SQL you might execute manually to verify the insertion of the data into the bookmark table. By coding this as a test, rather than simply performing it once by hand, you can continue to verify it is taking place each time you run the tests.



 
 
>>> More PHP Articles          >>> More By php|architect
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: